【问题标题】:Redirect to same/different html page using JavaScript使用 JavaScript 重定向到相同/不同的 html 页面
【发布时间】:2014-06-24 03:51:36
【问题描述】:

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

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


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

2. 当请求来自 Second.html 时,我必须转到同一页面,即 Second.html。 second.html 的 div 内容是使用 JavaScript 动态变化的,但页面是一样的。


我尝试了以下选项:

  1. window.location.href
  2. document.location.href
  3. location.href
  4. window.url
  5. window.location.replace(url)
  6. location.assign(url);

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 太棒了!下一步是什么?
  • 那么,你的 JavaScript 和/或 jQuery 在哪里?你被困在哪里了?您希望我们帮助做什么?

标签: javascript jquery html


【解决方案1】:

我自己解决了。

在first.html中

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

在 Second.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>

在js文件中

按钮上有一个事件是通过调用 someFunction() 来实现的,

$(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;
}

【讨论】:

    猜你喜欢
    • 2016-09-03
    • 2012-06-18
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多