0

I have created a button using Javascript and I am trying to open a window with window.open(url). The "url" opens up correctly initially. But then I created another button and did the same as I did earlier but this time around the url redirection got messed up. Both the buttons now open up on the last url that I used. I want both the buttons to work exclusively and open the respective urls that have been mentioned in the code.

  <div style="float:left;padding-right:30px;padding-bottom:30px;">
  <div>
 <img src="http://3.bp.blogspot.com/-7zqUR_RPEH8/VLxzHDIQXoI/AAAAAAAAAkM/388tnESCv9g/600/peacock-aeromech.jpeg" height="200px" 
  width="200px"/>
 </div>
  <div>
  <button onclick="myFunction()">Try it</button>
  <script type="text/javascript">
  function myFunction() {
  var btn = document.createElement("BUTTON");
  window.open("http://dl.amazon.com/dl/peacock-aeromech/p/itmdzdt4jdseczzh?pid=BLCDCYYHCKHVJDJG&srno=t_1&query=Lego&offer=b%3Amp%3Ac%3A112f129912.&affid=saurabhsu1","_self")
  }
  </script>
  </div>
  </div>

  <div style="float:left;padding-right:30px;padding-bottom:30px;">
  <div>
  <img src="http://3.bp.blogspot.com/-7OoAscM_6WI/VLxzGLzimNI/AAAAAAAAAj0/L424sw9xDIg/s1600/lego-school-bus.jpeg" height="200px" width="200px"/>
  </div>

   <div>
   <button onclick="myFunction()">Try it</button>
   <script type="text/javascript">
   function myFunction1() {
   var btn = document.createElement("BUTTON");
   window.open("http://dl.amazon.com/dl/lego-school-bus/p/itmdxsxgj7gfdzgk?pid=BLCDXSXFP3UTMYEX&srno=t_1&query=Lego&offer=b%3Amp%3Ac%3A11eaee7b19.&affid=saurabhsu1","_self")
   }
  </script>
  </div>
  </div>
7
  • 1
    Are you sure the code is correct. Both your buttons are calling myFunction(). Shouldnt that be myFunction1() for second button. Commented Jan 19, 2015 at 8:39
  • from where you are calling function1 Commented Jan 19, 2015 at 8:39
  • Why do you create a button in your code. It doesn't do anything and not visible to the user as well. Commented Jan 19, 2015 at 8:40
  • @Vishwanath even if u make it myFunction1() the thing doesn't work. Commented Jan 19, 2015 at 8:41
  • 1
    Some of the links you got does not seem to work. I don't think its a redirection problem, its rather the links that does not work. And you can remove the line var btn = document.createElement("BUTTON"); since its not being used Commented Jan 19, 2015 at 8:57

2 Answers 2

2

You have used same js function in both the buttons. to avoid confusions keep js at one place

<div style="float:left;padding-right:30px;padding-bottom:30px;">
	<div>
		<img src="http://3.bp.blogspot.com/-7zqUR_RPEH8/VLxzHDIQXoI/AAAAAAAAAkM/388tnESCv9g/s1600/peacock-aeromech.jpeg" height="200px" width="200px"/>
	</div>
	<div>
		<button onclick="myFunction()">Try it</button>
	</div>
</div>
<div style="float:left;padding-right:30px;padding-bottom:30px;">
	<div>
		<img src="http://3.bp.blogspot.com/-7OoAscM_6WI/VLxzGLzimNI/AAAAAAAAAj0/L424sw9xDIg/s1600/lego-school-bus.jpeg" height="200px" width="200px"/>
	</div>
	<div>
		<button onclick="myFunction1()">Try it</button>
	</div>
</div>

<script type="text/javascript">
   function myFunction() {
  	 window.open("http://dl.amazon.com/dl/peacock-aeromech/p/itmdzdt4jdseczzh?pid=BLCDCYYHCKHVJDJG&srno=t_1&query=Lego&offer=b%3Amp%3Ac%3A112f129912.&affid=saurabhsu1","_self")
   }
   function myFunction1() {
   	window.open("http://dl.amazon.com/dl/lego-school-bus/p/itmdxsxgj7gfdzgk?pid=BLCDXSXFP3UTMYEX&srno=t_1&query=Lego&offer=b%3Amp%3Ac%3A11eaee7b19.&affid=saurabhsu1","_self")
   }
</script>

Sign up to request clarification or add additional context in comments.

5 Comments

Maybe add some explanation about the changes you made. Or a description about where the question asker went wrong.
@Tom I have written ample to make someone understand.
@Somendra Beautiful ! Seperating javascript from the code is the best way to do it.
@androidrill Hi, I was commenting on the answer posted by Somendra, not on your original question.
@Tom No issues brother.Thanks for contributing.
1

I tried executing the code you have provided and by changing the function call from the second button to call myFunction1 rather than myFunction, I was able to access the two urls separately.

<html>

<head>
</head>

<body>
  <div style="float:left;padding-right:30px;padding-bottom:30px;">
    <div>
      <img src="http://3.bp.blogspot.com/-7zqUR_RPEH8/VLxzHDIQXoI/AAAAAAAAAkM/388tnESCv9g/600/peacock-aeromech.jpeg" height="200px" width="200px" />
    </div>
    <div>
      <button onclick="myFunction()">Try it</button>
      <script type="text/javascript">
        function myFunction() {
          var btn = document.createElement("BUTTON");
          window.open("http://dl.amazon.com/dl/peacock-aeromech/p/itmdzdt4jdseczzh?pid=BLCDCYYHCKHVJDJG&srno=t_1&query=Lego&offer=b%3Amp%3Ac%3A112f129912.&affid=saurabhsu1", "_self")
        }
      </script>
    </div>
  </div>

  <div style="float:left;padding-right:30px;padding-bottom:30px;">
    <div>
      <img src="http://3.bp.blogspot.com/-7OoAscM_6WI/VLxzGLzimNI/AAAAAAAAAj0/L424sw9xDIg/s1600/lego-school-bus.jpeg" height="200px" width="200px" />
    </div>

    <div>
      <button onclick="myFunction1()">Try it</button>
      <script type="text/javascript">
        function myFunction1() {
          var btn = document.createElement("BUTTON");
          window.open("http://dl.amazon.com/dl/lego-school-bus/p/itmdxsxgj7gfdzgk?pid=BLCDXSXFP3UTMYEX&srno=t_1&query=Lego&offer=b%3Amp%3Ac%3A11eaee7b19.&affid=saurabhsu1", "_self")
        }
      </script>
    </div>
  </div>
</body>

</html>

I attached the code here for your reference. However, it is same as yours, apart from the invocation of the javascript function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.