简体   繁体   English

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

[英]Redirect to same/different html page using JavaScript

I'm facing problem in redirecting to html page. 我在重定向到html页面时遇到问题。 My scenario is : 我的情况是:

I have two pages named First.html & Second.html. 我有两个页面分别名为First.html和Second.html。 Second.html contains a link to go back ie previous page. Second.html包含一个链接,可以返回上一页。 i have define <b>href=""</b> attribute of that link. 我已经定义了该链接的<b>href=""</b>属性。 And i call a JavaScript function goBack() on it's onClick() event. 我在onClick()事件上调用JavaScript函数goBack() And in that I written code to redirect using <b>window.location.href</b> . 然后,我编写了使用<b>window.location.href</b>进行重定向的代码。 Bcoz, I want to do something like below : Bcoz,我想做以下事情:


1. When request came from First.html, i have to go back to First.html from Second.html 1.当请求来自First.html时,我必须从Second.html返回至First.html。

2. When request came from Second.html, i have to go to same page, ie, Second.html. 2.当请求来自Second.html时,我必须转到同一页面,即Second.html。 Second.html's div's contents are changes dynamically using JavaScript, but the page is same. Second.html的div内容是使用JavaScript动态更改的,但是页面是相同的。


I have tried following options : 我尝试了以下选项:

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

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks 谢谢

i got solution by myself. 我一个人解决了。

In first.html 在first.html中

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

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

In js file 在js文件中

There is an event on button which come by calling someFunction(), in that 按钮上有一个事件,通过调用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM