user5603796
user5603796

Reputation: 389

jQuery check if URL contains string

I have this code

events.on('loaded', function() {
    $('.server_details .start button').click();
});

but I only want it to run if the end of the URL is &autoconnect, can someone show example of how to do this?

Example URL http://www.example.com:7778&autoconnect

Upvotes: 0

Views: 2516

Answers (3)

TheBilTheory
TheBilTheory

Reputation: 408

This would be more correct I suppose.

$(window).on('load', function () {
    if (window.location.href.indexOf('url-content') > -1) {
    // Your function do be executed
    }
});

Upvotes: 0

omarjmh
omarjmh

Reputation: 13896

You can get the url with window.location.href

and then check that using indexOf:

events.on('loaded', function() {
    if (window.location.href.indexOf("&autoconnect") > -1) {
       $('.server_details .start button').click();
    }
});

Upvotes: 2

vahanpwns
vahanpwns

Reputation: 963

You say "I only want it to run if the end of the URL is &autoconnect"

I say

var url = 'http://www.example.com:7778/&autoconnect';
var param = '&autoconnect';
var valid = url.indexOf(param) == url.length - param.length;

If there is a possibility that there may be other parameters as well...

var url = 'http://www.example.com:7778/&autoconnect&alsovalid';
var param = '&autoconnect';
var valid = url.indexOf(param) >= 0;

Upvotes: 0

Related Questions