www.webdeveloper.com
Results 1 to 3 of 3

Thread: How to separate/stop scripts?

  1. #1
    Join Date
    Dec 2012
    Posts
    3

    How to separate/stop scripts?

    This is my first time doing any coding of any type and to be honest I don't even know the proper terms for things

    I'm using this code to open two webpages with one link and it works great! However, when I have two of these on the same page, when I click "Link 1" the webpages from "Link 2" open. Then I tried adding a script for a "Link 3" and when I clicked either "Link 1" or "Link 2" only the webpages for "Link 3" opened.
    How do I stop this from happening? Do I need to separate the scripts? Stop them? Your help would be greatly appreciated!!
    PHP Code:
    <html>
    <
    head>
    <
    script type="text/javascript">
    function 
    open_win() {
    window.open("http://www.facebook.com/")
    window.open("http://www.google.com/")
    }
    </script>
    </head>
    <body>
    <form>
    <input type=button value="Link 1" onclick="open_win()">
    </form>
    </body>
    </html> 

    <html>
    <head>
    <script type="text/javascript">
    function open_win() {
    window.open("http://www.yahoo.com/")
    window.open("http://www.msn.com/")
    }
    </script>
    </head>
    <body>
    <form>
    <input type=button value="Link 2" onclick="open_win()">
    </form>
    </body>
    </html> 

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Two functions with the same name means one overwrites the other. The cheap answer is to rename one of the functions; the gentrified answer is to have just one function that cen be passed data:
    Code:
    <!doctype html5>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    
    function open_win() 
    {
      var len = arguments.length,
          offset = 50;
      
      for( var i = 0; i < len; i++, offset += 100 )
      {
        window.open( arguments[ i ], "w"+i, "top="+ offset +", left="+ offset +",width=" +(screen.width/2)+",height="+(screen.height/2) );       
      }
    }
    
    </script>
    </head>
    <body>
    <form>
     <input type=button value="Link 1" onclick="open_win('http://www.facebook.com/','http://www.google.com/')">
     <input type=button value="Link 2" onclick="open_win('http://www.yahoo.com/', 'http://www.msn.com/')">
    </form>
    </body> 
    </html>
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Dec 2012
    Posts
    3
    Thank you very much! That worked great!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles