javascript - 如何在JavaScript中改变window.location.href然后执行更多的JS?

标签 javascript jquery window.location

我有代码片段,它是在点击如下链接时执行的

cj_redirecturl="/HomeWork/main/load_course";
    utility.xhttprw.data(cj_redirecturl,{data:courseid},function(response){
        if(response){
            window.location.href=base_url+"main";

///next
utility.xhttprw.data(redirecturl,{data:""},function(response){
        if(response){

            id=ent;
            document.getElementById('content').innerHTML=response;
             // Assignemnt module
             //Call function from javascript/assignment.js file to display particular assignment  (ent:means assignment_id) //  
                if(con==5)
                {
                    get_assignment_id(ent);
                }
                if(con==9)
                {
                    get_content_details(ent);
                }


        }   //end of response       
    },false,false,false,'POST','html',true); 
        }
    },false,false,false,'POST','html'); 

在上面的代码中,window.location.href=base_url+"main";将页面重定向到其各自的页面,但这会停止执行紧挨着它编写的代码。现在我希望这段代码在编写时执行,即首先代码应该将我带到相应的“主”页面,然后必须执行之后编写的代码并给我所需的输出。

有人可以指导我实现这个目标吗?

最佳答案

window.location.href = base_url + "main"; <- 当您加载此页面时,调用您在 ///next 处定义的代码

你必须添加一些参数:

window.location.href=base_url+"main?parameter=true";

另一种方法是使用 ajax 将页面加载到 html 中的 div 中。

看看来自 jQuery 的 $.ajax()

关于javascript - 如何在JavaScript中改变window.location.href然后执行更多的JS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8209626/

相关文章:

javascript - Jquery自动完成源php函数

javascript - 文本字段不是通过单击按钮读取值,而是直到我将值物理添加到文本字段中

javascript - 使用 jquery/javascript 删除 span

javascript - 为什么不将全局变量值传递给函数?

javascript - window.location 从 localStorage 中删除了 token

javascript - 从浏览器使用 DynamoDB 的最简单方法是什么?

javascript - 在removeClass JQuery之后类没有被删除

javascript - 错误 : 3 INVALID_ARGUMENT: Cannot convert an array value in an array value in Firestore admin

javascript - location.href 在 ajax 之后不起作用(更多详细信息)

javascript - 如何从 JavaScript 函数重建 URL?