Nick Darley
Nick Darley

Reputation: 349

Simulate Link Click from Function

I am working on a webpage and I want to simulate a link click.

The way i have it setup is a user will click a link from an eblast that we are sending out and when the page loads the video will pop up of the link the picked.

Here is the website, and if you click on a image or title it pops up into a popup box. I am using prettyPhoto for this. http://dynamicdevsite.com/cmdnyc/audio-post-production-nyc.php

I have URL parser setup so my links look like this http://dynamicdevsite.com/cmdnyc/audio-post-production-nyc.php?ComedyCentral and the url parser sees ComedyCentral and then fires a function I have associated with that term and that fires just fine.

Code for link click simulation

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"));

}

ERROR
Uncaught TypeError: Cannot read property 'ownerDocument' of null

Note: I am sorry if this question is too localized, but I didn't know who/where else to ask at this point.

Upvotes: 0

Views: 1724

Answers (1)

BrunoLM
BrunoLM

Reputation: 100312

I use this code to do that

// 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);

The first line should fix your code.

Then change

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

to

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

This should create the correct event and fix your

Uncaught TypeError: Cannot read property 'ownerDocument' of null

error.

Upvotes: 1

Related Questions