Click to See Complete Forum and Search --> : onClick Function in browser URL
aaronking
September 6th, 2008, 02:08 AM
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.
javajawa
September 6th, 2008, 02:44 AM
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)
javascript:alert('Hello');
But you can only use the basic functions...
HanneSThEGreaT
September 6th, 2008, 05:05 AM
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/showpost.php?p=1471416&postcount=1
Xeel
September 8th, 2008, 03:29 PM
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>
<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');
mmetzger
September 8th, 2008, 07:25 PM
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.
Xeel
September 8th, 2008, 07:29 PM
btw =)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.