我有代码片段,它是在点击如下链接时执行的
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/