4 Replies - 6181 Views - Last Post: 28 September 2011 - 07:29 PM

#1 GodinHell  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 06-September 11

Javascript code to open Link in New window after Confirmation

Posted 11 September 2011 - 04:44 AM

Hi, I cannot write the small javascript, Before let me tell you, I am scriptkiddie in javascript and i dont have much knowledge of it. Here is my attempt, and i failed everytime.

Intention of Javascript code :- To Open link in new window, if clicked ok, else to open link in same tab.

<script type="text/javascript">
function show_confirm()
{
var awesome=confirm("Ok to Open Link in New tab, Cancel to open link in Same window!");
if (awesome==true)
  {
  window.location = "<base target='_blank'>";
  }
else
  {
  window.location = "<base target='_parent'>";
  }
}
</script>



Thanks

This post has been edited by GodinHell: 11 September 2011 - 04:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Javascript code to open Link in New window after Confirmation

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1490
  • View blog
  • Posts: 8,217
  • Joined: 07-September 06

Re: Javascript code to open Link in New window after Confirmation

Posted 12 September 2011 - 07:22 AM

window.location should be given a url, not HTML. That will help out that part of your code. Then, if you are attempting to open the link in a new tab (which is basically a new window as far as most browsers are concerned) you will want to use window.open.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 GodinHell  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 06-September 11

Re: Javascript code to open Link in New window after Confirmation

Posted 12 September 2011 - 09:19 AM

View PostBetaWar, on 12 September 2011 - 07:22 AM, said:

window.location should be given a url, not HTML. That will help out that part of your code. Then, if you are attempting to open the link in a new tab (which is basically a new window as far as most browsers are concerned) you will want to use window.open.
Hope that helps.


Actually, I am concerned with, all links in 1 page, and not any particular url.

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1490
  • View blog
  • Posts: 8,217
  • Joined: 07-September 06

Re: Javascript code to open Link in New window after Confirmation

Posted 12 September 2011 - 09:26 AM

So, I would change your show_confirm function to add a url parameter and then get all the links on the page, loop through them and tell them to call the show_confirm function when they are clicked, sending it their urls.

Here is how you get all the links:
var links = document.getElementsByTagName("a");



And then you loop through them liks so:
for(var i = 0, len = links.length; i < len; i++){
  var l = links[i];
  l.href = "javascript:show_confirm(" + l.href + ");";
}



After that code is run all links should call the show_confirm function sending their link href to it as a parameter.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#5 yusf22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-September 11

Re: Javascript code to open Link in New window after Confirmation

Posted 28 September 2011 - 07:29 PM

    if (awesome == true)
      {
        window.open('your page','_blank');
      }
    else
      {
        window.location = ("your page");
      }



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1