javascript 如何使用java脚本打开新的html文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22803634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:48:46  来源:igfitidea点击:

How to open new html file using java script

javascriptjqueryhtml

提问by Gajju

Suppose I have two html files: 'page1' and 'page2'

假设我有两个 html 文件:'page1' 和 'page2'

Code in page1:

第 1 页中的代码:

<html>

<body>
page1
<a href="page2.html" target="_blank"> click to go to page 2</a>
</body>

</html>

Code in page2:

第2页中的代码:

<html>

<body>
page2
<a href="page1.html" target="_blank"> click to go to page 1 </a>
</body>

</html>

Using this I can open page2 using hyperlink in page1 in a new tab in the same window and viceversa

使用它,我可以在同一窗口的新选项卡中使用 page1 中的超链接打开 page2,反之亦然

Is it possible to the same thing using javascript

是否可以使用 javascript 做同样的事情

回答by Govind Singh

lots of way possible ..

很多方法可能..

1.

1.

<script language="javascript" type="text/javascript">
    window.location.href="login.jsp?backurl="+window.location.href;
</script>

2.

2.

<script language="javascript">
    alert("back");
    window.history.back(-1);
</script>

3.

3.

<script language="javascript">
    window.navigate("top.jsp");
</script>

4.

4.

<script language="JavaScript">
    self.location="top.htm";
</script>

5.

5.

<script language="javascript">
    alert("Access Violation");
    top.location="error.jsp";
</script>

6.

6.

<script language="javascript">
    window.location = window.location.host;
</script>

回答by dakab

You can add an clickevent listener to your link element (or any other) and set the hrefproperty in the locationobject. Let's use inline onclickfor illustration:

您可以click向链接元素(或任何其他元素)添加事件侦听器并hreflocation对象中设置属性。让我们使用内联onclick来说明:

<a href="index.html" onclick="location.href='index.html';return false;">Link</a>

By clicking on this element, the onclickhandler will be executed, setting the href, i.e. the current address. By returning false, the default action will be prevented (see MouseClick.preventDefault()).

通过单击此元素,onclick将执行处理程序,设置href,即当前地址。通过返回false,将阻止默认操作(请参阅MouseClick.preventDefault())。

This is kind of a JavaScript surrogate for HTML hypertext behavior. You also can do other cool things with Window.location.

这是 HTML 超文本行为的一种 JavaScript 代理。您还可以使用Window.location做其他很酷的事情。

回答by Aameer

check this question here.you can use

在这里检查这个问题你可以使用

// similar behavior as an HTTP redirect
window.location.replace("http://stackoverflow.com");

// similar behavior as clicking on a link
window.location.href = "http://stackoverflow.com";