2013-06-18 24 views
-1

我需要創建一個彈出窗口,可在頁面加載時打開並且僅在美國東部時間上午11點至晚上7點之間打開。
我有自動彈出工作,但我添加時間差異麻煩。
請指教。 EDITED基於一天中的時間的Javascript自動彈出

<!--chat--> 
<script type="text/javascript"> 
    $(document).ready(function() { 
    var currentTime = new Date((new Date()).toUTCString()); 
    var startTime = new Date((new Date()).toUTCString()); 
    var endTime = new Date((new Date()).toUTCString()); 

    startTime.setUTCHours(8); 
    endTime.setUTCHours(19); 

    //set times to EST. (UTC -5) 
    currentTime.setHours(currentTime.getHours() - 5); 
    startTime.setHours(startTime.getHours() - 5); 
    endTime.setHours(endTime.getHours() - 5); 

    //code to determine your variables goes here 
    var mylink = "https://mylink.com"; 
    var windowname = "Chat Client"; 

    if (currentTime < endTime && currentTime > startTime) { 
     popup(mylink, windowname); 
    } 
    }); 

function popup(mylink, windowname) 
{ 
if (! window.focus)return true; 
var href; 
if (typeof(mylink) == 'string') 
    href=mylink; 
else 
    href=mylink.href; 
window.open(href, windowname, 'width=470,height=700,scrollbars=yes'); 
return false; 
} 
</SCRIPT> 
+0

請記住,大多數瀏覽器會在加載時阻止彈出窗口。 – RoToRa

+0

感謝RoToRa - 我解釋了完全相同的事情 - 但客戶端可重複地要求做到這一點。 – jes

回答

0
//bad code removed to save space 

編輯:

在任何時區工作:

//bad code removed to save space 

更新:

新增jQuery的標籤和函數定義演示代碼結構爲每個請求。

UPDATE2:

$(document).ready(function() {  
    var currentTime = toUTC(new Date()); 
    var startTime = toUTC(new Date()); 
    var endTime = toUTC(new Date()); 

    startTime.setHours(11); 
    startTime.setMinutes(0); 
    startTime.setSeconds(0); 
    endTime.setHours(19); 
    endTime.setMinutes(0); 
    endTime.setSeconds(0); 

    //set times to EST. (UTC -4) 
    currentTime.setHours(currentTime.getHours() - 4); 

    //code to determine your variables goes here 
    //var mylink = 
    //var windowname = 

    if (currentTime < endTime && currentTime > startTime) { 
    popup(); 
    } 
}); 

function popup() { 
    //put function code here 
    $('body').append("Here is some text"); 
} 

function toUTC(inDate) { 
    inDate.setMinutes(inDate.getMinutes() + inDate.getTimezoneOffset()); 
    return inDate; 
} 

在這裏,它是在一個fiddle

+0

原諒我,因爲我不像一些經驗豐富,但我需要添加我的代碼之後......如果是這樣,它究竟應該如何構造? – jes

+0

編輯我的回覆。希望能回答你的問題。如果您有更多問題,請告訴我! – chockleyc

+0

謝謝Chockleyc!我最初嘗試過這種方式,並且自動加載仍然發生 - 儘管它超出了指定的時間範圍....我發佈了上面的新代碼以供參考。 – jes

0

但它只適用於當地時間。

var date = new Date(); 
var hours = date.getHours(); 

if(hours > 11 && hours < 19) 
{ 
    // show pop-up 
}