Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JavaScript重定向到相同/不同的html页面_Javascript_Jquery_Html - Fatal编程技术网

使用JavaScript重定向到相同/不同的html页面

使用JavaScript重定向到相同/不同的html页面,javascript,jquery,html,Javascript,Jquery,Html,我在重定向到html页面时遇到问题。我的设想是: 我有两个名为First.html和Second.html的页面。html包含一个返回的链接,即上一页。我已经定义了该链接的href=”“属性。我在它的onClick()事件上调用一个JavaScript函数goBack()。在这篇文章中,我使用window.location.href编写了重定向代码。Bcoz,我想做如下事情: 1。当请求来自First.html时,我必须从Second.html返回First.html <a href="

我在重定向到html页面时遇到问题。我的设想是:

我有两个名为First.html和Second.html的页面。html包含一个返回的链接,即上一页。我已经定义了该链接的
href=”“
属性。我在它的
onClick()
事件上调用一个JavaScript函数
goBack()
。在这篇文章中,我使用
window.location.href
编写了重定向代码。Bcoz,我想做如下事情:


1。当请求来自First.html时,我必须从Second.html返回First.html

<a href="" onclick=" return goBack()">Back</a>
<script>
    var p = window.location.href;
    var pre = p.substring(p.lastIndexOf('?') + 3, p.length);
    var same = "";
    //It changes the contents of Second.html' div tag
    someFunction()
</script>
2.当请求来自Second.html时,我必须转到同一页面,即Second.html。html的div内容是使用JavaScript动态更改的,但页面是相同的


我尝试了以下几种选择:

  • window.location.href
  • document.location.href
  • location.href
  • window.url
  • window.location.replace(url)
  • location.assign(url)
  • 任何帮助都将不胜感激


    谢谢,我自己找到了解决办法

    在first.html中

    <a href="Second.html?p=First">
    

    令人惊叹的!接下来是什么?那么,您的JavaScript和/或jQuery在哪里?你被困在哪里?你想让我们帮什么忙?
    $(document).on('click', '.button1', function() {
        // some extra code
        same = "Second";
    }
    
    var diff;
    function goBack() {
        if (same == "First") {
            diff = pre;
            pre = same;
        }
        if (pre == same) {
            location.href = "Second.html?p=" + diff;
            same = "";
        } else if (pre != 'Second') {
            location.assign("" + pre + ".html");
            diff = pre;
        } else {
            location.assign("" + diff + ".html");
        }
        return false;
    }