重定向到特定页面并在js中编写该页面的代码

时间:2017-02-22 08:39:35

标签: javascript jquery html javascript-events

在重定向另一页时,应该执行一些代码,但不可能

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();  
    }  
  });  
}  

1 个答案:

答案 0 :(得分:1)

您的问题是,您正在尝试在加载其他页面时在页面内执行代码。你不能这样做(你可以在单页应用程序中做到这一点,但这是另一个故事)。

$(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(); }); 中,您应该添加以下代码:

/* 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();  
    }  
  }); 

您应该删除页面中的这部分代码: var test =“someotherpage.php”;

if (something === true) {
  /* Redirect to someotherpage.php */  
  window.location.href = "someotherpage.php?some_condition_you_want_to_check=1"
}  

编辑: 根据OP的要求,这里有一个澄清: 在您的第一页中,您可以执行以下操作:

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(); }); } }

mongodb

当然,我的代码是未经测试的,肯定会被改进的部分,但这应该让你知道你需要做什么来实现你想要的。