Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts

    create a link that runs a js command

    ok, im not sure if this is possible. I know that you can run a js command from the address bar, by typing something like javascript:alert('hello');. However, I would like to create a link that can open a webpage and run that js command all together at the same time.

    something like:
    Code:
    <a href="http://www.google.com/;javascript:alert('hello');">Click here</a>
    
    or
    
    <a href="http://www.google.com/&javascript:alert('hello');">Click here</a>
    except obviously those won't work.

    any suggestions are greatly appreciated. thanks.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    For security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.

    You can do this:-

    Code:
    <a href="#" onclick = "showMessage()" >Click here</a>
    
    <script type = "text/javascript">
    function showMessage() {
    alert ("Hello");
    window.location = "http://www.google.com"
    }
    </script>

  • #3
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts
    thanks for the reply... bummer though.


  •  

    Posting Permissions

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