2013-06-03 103 views
0

我在網頁上工作,我想模擬鏈接點擊。模擬鏈接點擊功能

我設置的方式是用戶將點擊我們發送的eblast中的鏈接,當頁面加載視頻時,會彈出鏈接摘取。

這裏是網站,如果你點擊圖片或標題,它會彈出一個彈出框。我正在使用prettyPhoto。 http://dynamicdevsite.com/cmdnyc/audio-post-production-nyc.php

我有URL解析設置,使我的鏈接看起來像這樣http://dynamicdevsite.com/cmdnyc/audio-post-production-nyc.php?ComedyCentral和URL解析器看到喜劇中心,然後觸發我曾與這個詞相關的功能和火災就好了。

代碼鏈接點擊模擬

function simulatedClick(target, options) { 

     var event = target.ownerDocument.createEvent('MouseEvents'), 
      options = options || {}; 

     //Set your default options to the right of || 
     var opts = { 
      type: options.type     || 'click', 
      canBubble:options.canBubble    || true, 
      cancelable:options.cancelable   || true, 
      view:options.view      || target.ownerDocument.defaultView, 
      detail:options.detail     || 1, 
      screenX:options.screenX     || 0, //The coordinates within the entire page 
      screenY:options.screenY     || 0, 
      clientX:options.clientX     || 0, //The coordinates within the viewport 
      clientY:options.clientY     || 0, 
      ctrlKey:options.ctrlKey     || false, 
      altKey:options.altKey     || false, 
      shiftKey:options.shiftKey    || false, 
      metaKey:options.metaKey     || false, //I *think* 'meta' is 'Cmd/Apple' on Mac, and 'Windows key' on Win. Not sure, though! 
      button:options.button     || 0, //0 = left, 1 = middle, 2 = right 
      relatedTarget:options.relatedTarget  || null, 
     } 

     //Pass in the options 
     event.initMouseEvent(
      opts.type, 
      opts.canBubble, 
      opts.cancelable, 
      opts.view, 
      opts.detail, 
      opts.screenX, 
      opts.screenY, 
      opts.clientX, 
      opts.clientY, 
      opts.ctrlKey, 
      opts.altKey, 
      opts.shiftKey, 
      opts.metaKey, 
      opts.button, 
      opts.relatedTarget 
     ); 

     //Fire the event 
     target.dispatchEvent(event); 
    } 

function CC_Lightbox() { 
     simulatedClick(document.getElementById("comedylink")); 

} 

錯誤
遺漏的類型錯誤:空的無法讀取屬性 'ownerDocument'

注:我,如果這個問題是很難過本地化,但我不知道在這一點上還有誰要問。

+0

JQuery是一個選項嗎? – Shenaniganz

+1

在第一個參數的某個地方有一個使用'null'的函數調用。 –

+0

好吧,似乎沒有像comedylink這樣的dom對象。你確定你使用'id'屬性來標識它而不是'name'嗎? – Sebas

回答

1

我用這個代碼做

// change to this line 
var evt = document.createEvent("MouseEvents"); 

evt.initMouseEvent('click',true,true,window,0,0,0,0,0,false,false,false,false,0,null); 
element.dispatchEvent(evt); 

第一行應該解決您的代碼。

然後改變

var event = target.ownerDocument.createEvent('MouseEvents'), 

var event = document.createEvent("MouseEvents"), 

這應該建立正確的事件並修復

Uncaught TypeError: Cannot read property 'ownerDocument' of null 

錯誤。

+0

這會讓我的燈箱彈出來嗎? –

+0

以及如何將其實施到我的功能? –

+0

我得到了這個錯誤是相同的,但在一個新的行上進一步下降未捕獲TypeError:無法讀取屬性'ownerDocument'爲null –