2017-02-22 25 views
0

在另一個頁面的重定向該頁面的代碼onload事件,有些代碼應該執行,但是這是不可能的重定向到特定網頁,並寫在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(); 
    } 
    }); 
} 
+1

一旦設置了window.location.href,它將停止執行JS。 – George

回答

1

你的問題是,你正試圖執行加載另一個頁面時頁面內的代碼。你不能這樣做(你可以在單頁應用程序中完成,但這是另一回事)。

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

當然,我的代碼是一個未經測試,肯定有待改進的部分,但是這應該讓你知道你需要做什麼來實現你想要的東西。

+0

嘿謝謝micheal,但我只需要執行那個加載事件if(something == true)條件。有什麼辦法可以做到嗎? –

+0

你應該在這樣的頁面中應用任何頁面特定的邏輯。如果您需要將頁1中的內容傳遞給page2,則可以從page1中添加GET參數並在page2中進行檢查。 –

+0

我在一個命名空間中編寫了代碼,所以** from **頁面中的**是隨機的,不知道如何將參數傳遞給其他頁面,您可以舉一個小例子。 –