0

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

2
  • use javascript regex. Commented Apr 9, 2016 at 16:47
  • Your example url should be http://www.example.com:7778/&autoconnect Commented Apr 9, 2016 at 16:49

3 Answers 3

2

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();
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

0

This would be more correct I suppose.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.