重定向到特定页面并在js中编写该页面的代码onload
Redirect to specific page and write code onload of that page in js
在重定向到其他页面时,应该执行一些代码,但这是不可能的
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 测试 = "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();
});
}
}
当然,我的代码是未经测试且肯定有待改进的部分,但这应该让您了解需要做什么才能实现您的目标。
在重定向到其他页面时,应该执行一些代码,但这是不可能的
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 测试 = "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();
});
}
}
当然,我的代码是未经测试且肯定有待改进的部分,但这应该让您了解需要做什么才能实现您的目标。