在重定向一页时,应该执行一些代码,但这是不可能的
if (something === true) {
/* Redirect to someotherpage.php */
window.location.href = "someotherpage.php"
var test = "someotherpage.php";
/* onload of someotherpage.php, execute this introjs step */
$(test).load(function() {
if (window.location.href === test) {
/* unable to execute this */
introJs().addStep({
element: $('#ck-dashboard-pending-task')[0],
intro: 'Here you can see the todo list',
position: 'left'
}).setOption('scrollToElement', true).start();
}
});
}
最佳答案
您的问题是,当加载另一个页面时,您试图在页面内执行代码。您不能以这种方式执行此操作(您可以在单页应用程序中执行此操作,但那是另一个故事)。
在您的 someotherpage.php
中,您应该添加以下代码:
$(window).load(function(){
introJs().addStep({
element:$('#ck-dashboard-pending-task')[0],
intro:'Here you can see the todo list',
position:'left'
}).setOption('scrollToElement', true).start();
});
您应该删除页面中的这部分代码: var test = "someotherpage.php";
/* onload of someotherpage.php, execute this introjs step */
$(test).load(function() {
if (window.location.href === test) {
/* unable to execute this */
introJs().addStep({
element: $('#ck-dashboard-pending-task')[0],
intro: 'Here you can see the todo list',
position: 'left'
}).setOption('scrollToElement', true).start();
}
});
编辑: 根据OP的要求,这里有一个澄清: 在您的第一页中,您可以执行以下操作:
if (something === true) {
/* Redirect to someotherpage.php */
window.location.href = "someotherpage.php?some_condition_you_want_to_check=1"
}
在someotherpage.php
上:
$(window).load(function(){
$.urlParam = function(name){
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results==null){
return null;
}
else{
return results[1] || 0;
}
}
if($.urlParam('some_condition_you_want_to_check') !== null){
introJs().addStep({
element:$('#ck-dashboard-pending-task')[0],
intro:'Here you can see the todo list',
position:'left'
}).setOption('scrollToElement', true).start();
});
}
}
当然,我的代码是未经测试且肯定有待改进的部分代码,但这应该会让您了解需要做什么才能实现您想要的目标。
关于javascript - 重定向到特定页面并在js中编写该页面的代码onload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42386459/