Have a question regarding URL and jQuery.
Can I specify URL to tell jQuery to run a function?
e.g http://www.website.com/about.html?XYZ
to run a function XYZ();
?
You can put code in that web page that examines the query parameters on the URL and then, based on what it finds, calls any javascript function you want.
In your particular example, a simplified version would be like this:
// code that runs when page is loaded:
if (window.location.search == "?XYZ") {
XYZ();
}
or if you want it to run any function that is present there, you can extract that from the string and run whatever name is there.
// code that runs when page is loaded:
if (window.location.search.length > 1) {
var f = window.location.search.substr(1); // strip off leading ?
try {
eval(f + "()"); // be careful here, this allows injection of javascript into your page
} catch(e) {/* handler errors here */}
}
Allowing arbitrary javascript to be run in your page may or may not have undesirable security implications. It would be better (if possible) to support only a specific set of pre-existing functions that you look for and know are safe rather than executing arbitrary javascript like the second example.
In the URL bar you can always put javascript:XYZ();
Try that after this url loads: http://jsfiddle.net/maniator/mmAxY/show/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With