admin管理员组文章数量:1291583
I am trying to make this work
<script>
document.write('<button onclick="location.href=' + document.referrer + '">Go back</button>');
</script>
It should be nothing but a simple "Back" button to the previsous page. When I do it like an "a href" it works
<script>
document.write('<a href="' + document.referrer + '">Go back</a>');
</script>
but when trying tom ake it as a "button" it fails. The button is there but didnt navigate to the previsious page
I am trying to make this work
<script>
document.write('<button onclick="location.href=' + document.referrer + '">Go back</button>');
</script>
It should be nothing but a simple "Back" button to the previsous page. When I do it like an "a href" it works
<script>
document.write('<a href="' + document.referrer + '">Go back</a>');
</script>
but when trying tom ake it as a "button" it fails. The button is there but didnt navigate to the previsious page
Share Improve this question asked Feb 12, 2014 at 21:57 Bocho TodorovBocho Todorov 4291 gold badge9 silver badges22 bronze badges 1- I think the problem is with the brackets and quotes...Please help :) – Bocho Todorov Commented Feb 12, 2014 at 21:58
3 Answers
Reset to default 5Add another escaped quote into the href body (as well as a semicolon at the end, although I don't know if that would make a difference) and see if that works...
<script>
document.write('<button onclick="location.href=\'' + document.referrer + '\';">Go back</button>');
</script>
You could also implement native back with:
window.history.back();
//in your onclick="history.back()"
//also can try with:
//javascript:history.go(-1)">
Or call a function that check if history.back() is available in the browser first else implement a custom back function/code as is showed here (I would prefer take out the code from the html just handle the onclick in my function)
Using javascript history.back() fails in Safari .. how do I make it cross-browser?
For a robust solution check history.js:
https://github./browserstate/history.js
You're missing the quotes around the URL in the href, so try escaping it like this (worked for me)
document.write('<button onclick="location.href=\'' + document.referrer + '\'">Go back</button>');
本文标签: javascriptHtml button with locationhref and documentreferrer togetherStack Overflow
版权声明:本文标题:javascript - Html button with location.href and document.referrer together - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741535323a2383991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论