-1

The target webpage sometimes contains a single embedded iframe video. I wish to open those specific videos in the current tab.

<iframe allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true" marginheight="0" marginwidth="0" scrolling="no" frameborder="0" width="100%" src="https://streamview.com/v/wkx5ntgwdv5b"></iframe>

My code:

(function() {
    'use strict';

    var openontaB = document.querySelector('iframe').src;
    window.location.href = openontaB;
})();

The issue is that the above code does not open the correct iframe src. How would I make it work/match only for an iframe that contains the string streamview.com?

​Thanks

4
  • 1
    Does this answer your question? How to check whether a string contains a substring in JavaScript? Commented Jan 24, 2024 at 7:09
  • var links = document.getElementById("iframe"); for (var i = 0, l = links.length; i < l; i++) { var link = links[i]; if (link.href.indexOf("streamview.com") !== -1) { (function() { 'use strict'; var openontaB = document.querySelector('iframe').src; window.location.href = openontaB; })(); } } still not working Commented Jan 24, 2024 at 13:04
  • Please see How to Ask, then revise your post title to ask a clear, specific question. Commented Jan 24, 2024 at 15:33
  • Were any of the below answers helpful to you? If so, please consider selecting one as the "best answer" (click the checkmark beside the answer). Doing that will acknowledge the answer and close out the question, which is helpful to the StackOverflow community. You receive points each time you close out a question. Commented Feb 13, 2024 at 15:06

2 Answers 2

1

You can also do something like this:

// look for an iframe with 'streamview.com' in its src
const iframe = document.querySelector('iframe[src*="streamview.com"]');
// if found, set location
iframe && (location.href = iframe.src);
Sign up to request clarification or add additional context in comments.

Comments

0

Try (untested):

(function() {
    'use strict';

    const allIF = document.querySelectorAll('iframe');

    allIF.forEach( frm => {
        const openontaB = frm.src;
        if (openontaB.includes('streamview.com')){
            window.location.href = openontaB;
        }
    });
})();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.