Javascript: open a new page in the same window

Is there an easy way to change this code so that the destination URL opens in the same window?

<a href="javascript:q=(document.location.href);void(open('http://example.com/submit.php?url='+escape(q),'','resizable,location,menubar,toolbar,scrollbars,status'));">click here</a>'' 
+57
javascript window
Nov 06 '08 at 5:13
source share
8 answers

The second parameter window.open () is a string representing the name of the target window.

Install it: "_self".

 <a href="javascript:q=(document.location.href);void(open('http://example.com/submit.php?url='+escape(q),'_self','resizable,location,menubar,toolbar,scrollbars,status'));">click here</a> 


Sidenote: The following question gives an overview of perhaps the best way to link event handlers to HTML links.

What is the best way to replace js function references?

+70
Nov 06 '08 at 5:21
source share
 <script type="text/javascript"> window.open ('YourNewPage.htm','_self',false) </script> 

see link: http://www.w3schools.com/jsref/met_win_open.asp

+76
Aug 24 '11 at 20:21
source share
 <a href="javascript:;" onclick="window.location = 'http://example.com/submit.php?url=' + escape(document.location.href);'">Go</a>; 
+7
Nov 06 '08 at 5:16
source share

try it, it worked for me in ie 7 and ie 8

  $(this).click(function (j) { var href = ($(this).attr('href')); window.location = href; return true; 
+3
Jul 19. '11 at 13:26
source share

Here is what worked for me:

 <button name="redirect" onClick="redirect()">button name</button> <script type="text/javascript"> function redirect(){ var url = "http://www.google.com"; window.open(url, '_top'); } </script> 
+3
Mar 23 '16 at 8:13
source share

I would take it a little differently if I were you. Change the text link when the page loads, rather than a click. I will give an example in jQuery, but it can be easily done in javascript (although jQuery is better)

 $(function() { $('a[href$="url="]') // all links whose href ends in "url=" .each(function(i, el) { this.href += escape(document.location.href); }) ; }); 

and write your HTML as follows:

 <a href="http://example.com/submit.php?url=">...</a> 

the advantages of this is that people can see what they click on (href is already installed) and it removes javascript from your HTML.

All this suggests that you are using PHP ... why not add it on the server side?

+1
Nov 06 '08 at 6:24
source share

So, adding the url at the end of href will each link open in the same window? Perhaps you can also use _BLANK in HTML to do the same.

+1
Mar 03 2018-11-11T00:
source share

try

 <a href="#" onclick="location='http://example.com/submit.php?url='+escape(location)" >click here</a> 
0
Apr 17 '19 at 4:17
source share



All Articles