onClick Function in browser URL
Hello,
Do you know if it is possible to call a Javascript function from a browser URL?
I have a javascript function which im trying to run and I want to be able to enter it in the address bar and run it..
I have it working when you click on a link using the 'onClick' event but I want to be able to put the 'onClick' event in the browser URL address bar and run the function.
Does anyone know how to do this?
Thanks.
Re: onClick Function in browser URL
Well, when you change the URL, the page will be unloaded, ad any javascript function in the page will no longer be availiable.
Otherwise, depending on your browser, this works
(Tested in Google Chrome, and (I think) IE5 & 6)
Code:
javascript:alert('Hello');
But you can only use the basic functions...
Re: onClick Function in browser URL
You can use more complicated stuff inside the addressbar as well - as long as you separate each Javascript statement with a semi colon ;)
Here's a funky example :
http://www.codeguru.com/forum/showpo...16&postcount=1
Re: onClick Function in browser URL
not sure if that's what you needed but I've made something interesting here. It takes a part of the string in the browser address bar as a parameter and executes it.
HTML Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<p>reading js from url</p>
</body>
<script type="text/javascript" language="javascript1.5" charset="utf-8">
function URLDecode (encodedString) {
var output = encodedString;
var binVal, thisString;
var myregexp = /(%[^%]{2})/;
while ((match = myregexp.exec(output))!=null && match.length>1 && match[1]!=''){
binVal = parseInt(match[1].substr(1),16);
thisString = String.fromCharCode(binVal);
output = output.replace(match[1], thisString);
}
return output;
}
var js = window.location.href;
js = js.substring(js.indexOf("js=")+3);
eval(URLDecode(js));
</script>
</html>
try running this.html?js=alert('javascript =)'); alert('2+2=4');
Re: onClick Function in browser URL
Just in case this is going live online - please be careful doing this type of thing - it can be the attack point for assorted types of security issues, including cross site scripting, etc.
Re: onClick Function in browser URL