CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2006
    Posts
    228

    Question 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.

  2. #2
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    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...
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    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

  4. #4
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    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');
    Last edited by Xeel; September 8th, 2008 at 03:32 PM.
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

  5. #5

    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.

  6. #6
    Join Date
    Jul 2005
    Location
    Currently in Mexico City
    Posts
    568

    Re: onClick Function in browser URL

    btw =)
    Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?

    I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)

    //always looking for job opportunities in AU/NZ/US/CA/Europe :P
    willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));

    USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured