foxer
foxer

Reputation: 901

Check if iframe has specific id using JQuery

I have a function to call another function inside am iFrame like this:

function globalFunc(value) {
    //select and add ID to the iFrame
    const preloading_iFrame = $("iframe[src*='http://127.0.0.1:8887/Components/preloading/index.html']");
    preloading_iFrame.attr("id","preloadingID");
    // call iframe's function
    document.getElementById('preloadingID').contentWindow.iframeFunc(value);
}

Since I want to execute the globalFunc multiple times, I was wondering if we can bypass adding id to the iframe after the first time it has been added.

Something like check if iframe has the preloadingID as the id then don't add it again!

Is there any solution?

Upvotes: 0

Views: 141

Answers (1)

Barmar
Barmar

Reputation: 781096

There's no need to add the ID. You have a reference to the element already, just use that.

function globalFunc(value) {
    //select and add ID to the iFrame
    const preloading_iFrame = $("iframe[src*='http://127.0.0.1:8887/Components/preloading/index.html']");
    // call iframe's function
    preloading_iFrame[0].contentWindow.iframeFunc(value);
}

Indexing a jQuery collection returns the corresponding DOM elements.

Upvotes: 1

Related Questions