How to alert when url contains[...]

How can I disable an input-field with jQuery using GM if the url contains text?

I tried

$(document).ready( function() {
    else {

I don't know what I did wrong. And I also don't know whether url:code is a regular expression to use.


Hi again, thanks for your time putting in this. I guess it's easier to ask/answer how to call an alert when the url contains e.g. "newthread".

So, we have got a url(e.g.): , and an input-field with id="subject" and a label for "subject". If the url contains "newthread" the input-and label-element should be hidden. Following code works, but not for me. The console says nothing and won't alert though.

var url = window.location.href 
url = "";
    var pos =; 
    if(pos >= 0)


Thank you to all helping me.

Edit inserts the final solution

if (location.href.indexOf("text") != -1) {
$('#fieldname').attr("disabled", true); 


Throw away that snippet. Should be like

     var url = window.location.href;

        $('#field-owner').attr('disabled', 'disabled');


"$ not defined" means you either aren't loading the jQuery library or $ gets overwritten. I put the whole snippet into a self-executing anonymous function which will fix the last issue at least.

url:contains isn't valid JavaScript, and it will cause a parse error. As with all jQuery selectors, you're actually passing a string for jQuery to parse:


That said, url: is not a valid jQuery selector. jQuery isn't actually the solution to this problem; instead use window.location.href to get the current URL, and test it with a regular expression:

if (window.location.href.match(/text/)) {
  // ...

The next problem is your code to enable/disable your input boxes. It's... beyond broken. Firstly, if the element has an ID, just use the #id selector. Secondly, selectors like :enabled are for filtering elements - pulling more specific elements from the DOM to work with. They don't actually change the state of the elements they select.

You want something like this:

if (window.location.href.match(/text/)) {
    // apply disabled attribute
    $('#field-owner').attr('disabled', 'disabled');
} else {
    // remove disabled attribute if it exists


RE: $ not defined:

You aren't including the jQuery library in your page. Make sure you've downloaded jQuery and successfully included it in your page via a <script> tag.

try something like this..

function getUrlVars()
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
        hash = hashes[i].split('=');
        vars[hash[0]] = hash[1];
    return vars;

and then you can do

if (getUrlVars()["text"] != null) {

Try this simple thing . Not sure this will suite you. But it will find for the word 'text' and disable the input

    var temp = $(this).val();
    var searchTerm = 'text';
    if('text') > 0 || == 0){
        $("#field-owner").attr('disabled', 'disabled');

just change the variable searchTerm and use it. Works for me. DEMO

Need Your Help

How to change datagridview column date format when gridview has autogeneratecolumns=true

c# .net gridview date-format

I am auto generating columns in gridview depending on search parameters, few columns will be added or removed.

iOS: Custom View for UITabBarItem

ios iphone uiview uitabbarcontroller uitabbaritem

I would like to be able to use a custom view as a UITabBarItem. I can't seem to find any information about this.