Javascript 如何制作 HTML 反向链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8814472/
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 make an HTML back link?
提问by paislee
What is the simplest way to create an <a>
tag that links to the previous web page? Basically a simulated back button, but an actual hyperlink. Client-side technologies only, please.
创建<a>
链接到上一个网页的标签的最简单方法是什么?基本上是一个模拟的后退按钮,而是一个实际的超链接。请仅使用客户端技术。
Edit
Looking for solutions that have the benefit of showing the URL of the page you're about to click on when hovering, like a normal, static hyperlink. I'd rather not have the user looking at history.go(-1)
when hovering on a hyperlink. Best I've found so far is:
编辑
寻找能够在悬停时显示您将要点击的页面的 URL 的解决方案,就像一个普通的静态超链接。history.go(-1)
当悬停在超链接上时,我宁愿不让用户查看。到目前为止我发现的最好的是:
<script>
document.write('<a href="' + document.referrer + '">Go Back</a>');
</script>
Is document.referrer
reliable? Cross-browser safe? I'll be happy to accept a better answer.
是否document.referrer
可靠?跨浏览器安全吗?我很乐意接受更好的答案。
采纳答案by paislee
This solution has the benefit of showing the URL of the linked-to page on hover, as most browsers do by default, instead of history.go(-1)
or similar:
此解决方案的好处是在悬停时显示链接页面的 URL,正如大多数浏览器默认情况下所做的那样,而不是history.go(-1)
或类似:
<script>
document.write('<a href="' + document.referrer + '">Go Back</a>');
</script>
回答by Bajrang
And another way:
而另一种方式:
<a href="javascript:history.back()">Go Back</a>
回答by apiguy
The easiest way is to use history.go(-1);
最简单的方法是使用 history.go(-1);
Try this:
尝试这个:
<a href="#" onclick="history.go(-1)">Go Back</a>
回答by Hemant Metalia
you can try javascript
你可以试试javascript
<A HREF="javascript:history.go(-1)">
refer JavaScript Back Button
EDIT
编辑
to display url of refer http://www.javascriptkit.com/javatutors/crossmenu2.shtml
显示参考网址 http://www.javascriptkit.com/javatutors/crossmenu2.shtml
and send the element a itself in onmouseover as follow
并在 onmouseover 中发送元素 a 本身,如下所示
function showtext(thetext) {
if (!document.getElementById)
return
textcontainerobj = document.getElementById("tabledescription")
browserdetect = textcontainerobj.filters ? "ie" : typeof textcontainerobj.style.MozOpacity == "string" ? "mozilla" : ""
instantset(baseopacity)
document.getElementById("tabledescription").innerHTML = thetext.href
highlighting = setInterval("gradualfade(textcontainerobj)", 50)
}
<a href="http://www.javascriptkit.com" onMouseover="showtext(this)" onMouseout="hidetext()">JavaScript Kit</a>
check jsfiddle
检查jsfiddle
回答by Vivek Maharajh
This solution gives you the best of both worlds
此解决方案为您提供两全其美的解决方案
- Users get to hover over the link to see the URL
- Users don't end up with a corrupted back-stack
- 用户可以将鼠标悬停在链接上以查看 URL
- 用户不会以损坏的返回堆栈结束
More details in the code comments below.
下面的代码注释中有更多详细信息。
var element = document.getElementById('back-link');
// Provide a standard href to facilitate standard browser features such as
// - Hover to see link
// - Right click and copy link
// - Right click and open in new tab
element.setAttribute('href', document.referrer);
// We can't let the browser use the above href for navigation. If it does,
// the browser will think that it is a regular link, and place the current
// page on the browser history, so that if the user clicks "back" again,
// it'll actually return to this page. We need to perform a native back to
// integrate properly into the browser's history behavior
element.onclick = function() {
history.back();
return false;
}
<a id="back-link">back</a>
回答by Vir
For going back to previous page using Anchor Tag <a>
, below are 2 working methods and out of them 1st oneis fasterand have one great advantagein going back to previous page.
对于使用锚标记回到前一页<a>
,下面是2种工作方法和从他们的第一个1是速度更快,并有一个很大的优势在回到前一页。
I have tried both methods.
这两种方法我都试过了。
1)
1)
<a href="#" onclick="location.href = document.referrer; return false;">Go Back</a>
Above method (1) works great if you have clicked on a link and opened link in a New Tabin current browser window.
如果您单击了一个链接并在当前浏览器窗口的新选项卡中打开了链接,则上述方法 (1) 效果很好。
2)
2)
<a href="javascript:history.back()">Go Back</a>
Above method (2) only works ok if you have clicked on a link and opened link in a Current Tabin current browser window.
上述方法 (2) 仅在您单击链接并在当前浏览器窗口的当前选项卡中打开链接时才有效。
It will not work if you have open link in New Tab. Here history.back()
will not work if link is opened in New Tab of web browser.
如果您在新标签页中打开了链接,它将不起作用。history.back()
如果在网络浏览器的新标签页中打开链接,这里将不起作用。
回答by Vir
A back link is a link that moves the browser backwards one page, as if the user had clicked the Back button available in most browsers. Back links use JavaScript. It moves the browser back one page if your browser supports JavaScript (which it does) and if it supports the window.history
object, which is necessary for back links.
后退链接是将浏览器向后移动一页的链接,就像用户单击了大多数浏览器中可用的后退按钮一样。反向链接使用 JavaScript。如果您的浏览器支持 JavaScript(它确实支持)并且如果它支持window.history
反向链接所必需的对象,它会将浏览器向后移动一页。
Simple ways are
简单的方法是
<a href="#" onClick="history.go(-1)">Go Back</a>
OR:
或者:
function goBack() {
window.history.back()
}
<a href="#" onclick="goBack()" />Go Back</a>
Generally speaking a back link isn't necessary… the Back button usually suffices quite nicely, and usually you can also simply link to the previous page in your site. However, sometimes you might want to provide a link back to one of several "previous" pages, and that's where a back link comes in handy. So I refer you below tutorial if you want to do in more advanced way:
一般来说,后退链接不是必需的……后退按钮通常就足够了,通常您也可以简单地链接到您网站的上一页。但是,有时您可能想要提供一个返回几个“前一个”页面之一的链接,这就是反向链接派上用场的地方。因此,如果您想以更高级的方式进行操作,我会在下面的教程中为您推荐:
http://www.htmlcodetutorial.com/linking/linking_famsupp_108.html
http://www.htmlcodetutorial.com/linking/linking_famsupp_108.html
回答by mack
try this
尝试这个
<a href="javascript:history.go(-1)"> Go Back </a>
回答by Roee Gavirel
<a href="#" onclick="history.back();">Back</a>
回答by Leonid Vasilev
You can also use history.back()
alongside document.write()
to show link only when there is actually somewhere to go back to:
您还可以使用history.back()
并排document.write()
显示链接,仅当确实有可以返回的地方时:
<script>
if (history.length > 1) {
document.write('<a href="javascript:history.back()">Go back</a>');
}
</script>