简体   繁体   English

重定向到新页面时代码未执行

[英]Code not executing when redirecting to new page

When i click on Buy Now button the next page appears but it is not showing the contents which want it to show.当我单击“立即购买”按钮时,会出现下一页,但没有显示想要显示的内容。 What is wrong with this code?这段代码有什么问题?

This is HTML code:这是 HTML 代码:

    <div class="card-body" style="height:260px;">
      <p class="card-text"> Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible</p>
    </div>
    <div class="card-footer" style="height:56px;">
      <center><a href="BuyCourse.html"><button type="submit" id="javabtn" onclick="BuyJava()" value="Java" class="btn btn-primary">Buy Now</Button></a></center>
    </div>

This is second page code which opens after clicking on Buy Now这是点击立即购买后打开的第二页代码

 <div class="container" id="containerJava">
        <div>
          <div>
            <label style="font-size: 20; margin-top:50px; margin-left: 5px;">Course Name :-</label>
            <p id="lblcourse"></p>
          </div>

          <div>
            <label style="font-size: 20; margin-left: 5px;">Course Duration :-</label>
            <p id="lblduration"></p>
          </div>

          <div>
            <label style="font-size: 20; margin-left: 5px;">Course Price :-</label>
            <p id="lblprice"></p>
          </div>

And this is the JavaScript code这是 JavaScript 代码

function BuyJava() {
    document.getElementById("lblcourse").innerHTML = "Java";
    document.getElementById("lblduration").innerHTML = "6 Months";
    document.getElementById("lblprice").innerHTML = "6000/-";
}

As you can see below, the page shows that the values haven't been inserted:如下所示,该页面显示尚未插入值: 在此处输入图片说明

You are using an a element which is redirecting the user to another page.您正在使用一个将用户重定向到另一个页面a元素。 The JavaScript will run on click of the button when the user is on the first page.当用户在第一页上时,JavaScript 将在点击按钮时运行。 However it will not run on the second page, as it has no way of knowing the code was supposed to be executed.但是它不会在第二页上运行,因为它无法知道应该执行的代码。 If you visit BuyCourse.html directly, do you expect a code to run?如果您直接访问BuyCourse.html ,您是否希望运行代码? No.不。

One way to make this work is by adding parameters to the URL of the GET call, then on the second page extract the parameters from the URL with a JavaScript code, and update the content of the page.实现这一目标的一种方法是向 GET 调用的 URL 添加参数,然后在第二个页面上使用 JavaScript 代码从 URL 中提取参数,并更新页面的内容。


Below is an example of such method: in the first page we have removed the a tag.下面是这种方法的一个例子:在第一页中,我们删除了a标签。 Instead we will handle the redirection with JavaScript.相反,我们将使用 JavaScript 处理重定向。 Instead of just redirecting to BuyCourse.html we will redirect to BuyCourse.html with some parameters.我们将使用一些参数重定向到BuyCourse.html 而不是仅仅重定向到BuyCourse.html A way to do this is making a GET call and adding parameters to the URL.一种方法是进行 GET 调用并向 URL 添加参数。 You can read more here about it.你可以在这里阅读更多关于它的信息。

For instance, here we are retrieving the value of the button that was clicked and adding it to the URL path.例如,这里我们检索被点击的按钮的值并将其添加到 URL 路径中。 You can have multiple parameters of course.当然,您可以有多个参数。 The URLSearchParams class will handle the conversion object -> url param object . URLSearchParams类将处理转换object -> url param object Once stringified URLSearchParams will look just like the path you want.一旦字符串化URLSearchParams看起来就像你想要的路径。

 function BuyJava(element) { // here you can pass as many parameters // as you want in a key -> value format const params = { value: element.value } // convert the param object into an URLSearchParams // object which is a built-in structure const searchParams = new URLSearchParams(data); // redirect the user to the new page with the params window.location = 'BuyCourse.html?' + searchParams }
 <div class="card-body" style="height:260px;"> <p class="card-text"> Java is a general-purpose programming language that is class-based, object-oriented, and designed to have as few implementation dependencies as possible</p> </div> <div class="card-footer" style="height:56px;"> <center><button type="submit" id="javabtn" onclick="BuyJava(this)" value="Java" class="btn btn-primary">Buy Now</button></center> </div>

When clicking on the button, it should redirect you to something like 'BuyCourse.html?value=Java' .单击按钮时,它应该将您重定向到类似'BuyCourse.html?value=Java'

On the second page you will have to retrieve the parameters... this time from the URL itself.在第二页上,您必须检索参数……这次是从 URL 本身检索。 You can get the URL of the current page with document.location then create an URL object and select the key you want:您可以使用document.location获取当前页面的 URL,然后创建一个URL对象并选择所需的键:

const url = new URL(document.location);
const value = url.searchParams.get('value');
document.getElementById("lblcourse").innerHTML = value;

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

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