简体   繁体   English

Javascript:在同一个窗口中打开新页面

[英]Javascript: open new page in same window

是否有一种简单的方法来修改此代码,以便在SAME窗口中打开目标URL?

 <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>`` 

<script type="text/javascript">
window.open ('YourNewPage.htm','_self',false)
</script>

see reference: http://www.w3schools.com/jsref/met_win_open.asp 参见参考: http//www.w3schools.com/jsref/met_win_open.asp

The second parameter of window.open() is a string representing the name of the target window. window.open()的第二个参数是表示目标窗口名称的字符串。

Set it to: "_self". 将其设置为:“_ 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 an arguably better way to bind event handlers to HTML links. 旁注:以下问题概述了将事件处理程序绑定到HTML链接的更好方法。

What's the best way to replace links with js functions? 用js函数替换链接的最佳方法是什么?

<a href="javascript:;" onclick="window.location = 'http://example.com/submit.php?url=' + escape(document.location.href);'">Go</a>;

Here's 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>

try this it worked for me in ie 7 and ie 8 试试这个在7和8中为我工作

 $(this).click(function (j) {
            var href = ($(this).attr('href'));
            window.location = href;
            return true;

I'd take that a slightly different way if I were you. 如果我是你,我会采取一种略微不同的方式。 Change the text link when the page loads, not on the click. 页面加载时更改文本链接,而不是单击。 I'll give the example in jQuery, but it could easily be done in vanilla javascript (though, jQuery is nicer) 我将在jQuery中给出示例,但它可以很容易地在vanilla javascript中完成(尽管,jQuery更好)

$(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 like this: 并像这样写你的HTML:

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

the benefits of this are that people can see what they're clicking on (the href is already set), and it removes the javascript from your HTML. 这样做的好处是人们可以看到他们点击的内容(href已经设置好),并从HTML中删除了javascript。

All this said, it looks like you're using PHP... why not add it in server-side? 所有这些说,看起来你正在使用PHP ...为什么不在服务器端添加它?

So by adding the URL at the the end of the href, Each link will open in the same window? 那么通过在href的末尾添加URL,每个链接将在同一个窗口中打开? You could also probably use _BLANK within the HTML to do the same thing. 您也可以在HTML中使用_BLANK来执行相同的操作。

try 尝试

 <a href="#" onclick="location='http://example.com/submit.php?url='+escape(location)" >click here</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM