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
How to open new html file using java script
提问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 click
event listener to your link element (or any other) and set the href
property in the location
object. Let's use inline onclick
for illustration:
您可以click
向链接元素(或任何其他元素)添加事件侦听器并href
在location
对象中设置属性。让我们使用内联onclick
来说明:
<a href="index.html" onclick="location.href='index.html';return false;">Link</a>
By clicking on this element, the onclick
handler 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做其他很酷的事情。