• 0

iFrame SRC from TextArea


Question

Hello gang,

 

I need to be able to display various content in an iFrame.  Below is some code that works, sometimes... hence this post.

 

Some urls work, such as http://www.cnn.com

Others do not, such as most "Share" urls from YouTube.   I have even pasted the Share code from one video to see if it works, nope

 

 document.getElementById("iContent").src = "http://youtu.be/wupToqz1e2g";

 

I've already spent some time on Google, but I thought I'd post here as well.

 

Thanks for all info

<html>
<body>
<script>
function LoadContent() {
    try {
        var sContent = document.getElementById("txtContent").value;
	   
        if (IsURL(sContent) == true) {
            document.getElementById("iContent").src = sContent;
        }
        else {
            document.getElementById("iContent").src = "http://www.youtube.com/embed/gLeoJxkyXg4?autoplay=1&controls=0&loop=1";
        }
    }
    catch (Err) {
        //logException(err);
    }
}

function IsURL(url) {
    //http://stackoverflow.com/questions/1701898/how-to-detect-whether-a-string-is-in-url-format-using-javascript
   
    try {
        var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
            + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" 
            + "(([0-9]{1,3}\.){3}[0-9]{1,3}" 
            + "|" 
            + "([0-9a-z_!~*'()-]+\.)*" 
            + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." 
            + "[a-z]{2,6})" 
            + "(:[0-9]{1,4})?" 
            + "((/?)|" 
            + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
        var re=new RegExp(strRegex);
        return re.test(url);
    }
    catch (Err) {
        //logException(Err);
    }
}
</script>	

<iframe id="iContent" width="420" height="315" src="https://www.youtube.com/embed/gLeoJxkyXg4?autoplay=1&controls=0&loop=1"></iframe>
<br/>
<textarea rows="3" cols="50" id="txtContent" onkeyup="LoadContent();" placeholder="Enter Something." ></textarea>
</body>
</html>
Link to comment
https://www.neowin.net/forum/topic/1215657-iframe-src-from-textarea/
Share on other sites

5 answers to this question

Recommended Posts

  • 0
  On 30/05/2014 at 02:07, Seahorsepip said:

Some sites like Facebook block iframes and there's no way to fix it.

 

 

Thanks, but that situation is not my concern.  You're right, for those sites that don't allow this there is nothing I can do.  However, for example a url from you tube (url below as example) when I paste it into my Facebook wall I get the preview picture, details, etc.   But with the above code it does nothing.  I am looking to recreate the same functionality as it happens in Facebook

 

 

 

http://www.youtube.com/watch?v=uDJPjLc9Xr4&feature=share&list=UUzwDRd4MlgucoBGNAzUEKHg&index=1

 

Thanks

  • 0
In the case of Facebook, when you paste a URL on your wall, Facebook extracts the preview image from the Open Graph image meta tag (if available), if you look in the source for that page you'll find this tag:

 



<meta property="og:image" content="https://i1.ytimg.com/vi/uDJPjLc9Xr4/maxresdefault.jpg">

  • 0

Yeah, you're going to have to parse the page on the server to pull out details like that, the client security model won't allow for it.

Also, that's a fairly sub-optimal way to validate a URL, I'd try using the methods provided by the browser (new URL('http://example.com/'); ) first, then fallback to something like that (And the browser object will sanitise and properly escape the URL to boot)

This topic is now closed to further replies.
  • Posts

    • Xbox July Update brings PC app cloud upgrades and Rewards support by Pulasthi Ariyasinghe The Xbox team at Microsoft has another major series of updates hitting its platforms. The Xbox July Update is primarily bringing new features to the PC application, while cloud gaming services are also being upgraded. A lot of these additions were a part of Insider testing sessions previously, but now they are ready for prime time. First off, Game Pass Ultimate members can now stream supported games over the cloud, as long as they own a copy on the Xbox store. The Stream Your Own Game feature can be accessed via the Cloud Gaming section on the Xbox PC app. The feature now boasts over 250 supported games too, with recent additions including classic Assassin's Creed titles, LEGO games, and the Saints Row series. Upcoming games to the lineup include RoboCop: Rogue City – Unfinished Business, Tetris Effect Connected, Wo Long Fallen Dynasty, and more. Check here to get a full list of games. Don't forget that cross-device play histories on the app also landed for Xbox Insiders earlier this month, letting players see what games they have been playing regardless of console, PC, or cloud Xbox platform being used. Another new feature announced today as landing on the PC app this month is Rewards with Xbox. Only available in select markets and only for those above 18, Rewards can now be found in the Home section with easy access to checking out how to get more points, track progress, and more. The Xbox and Antstream Arcade joint venture, the Retro Classics app, is gaining seven more games too. These are Caesar, Conquests of Camelot: The Search for the Grail, Gabriel Knight: Sins of the Fathers, Hard Head, Okie Dokie, Skate Boardin’, and Skeleton+. Lastly, mouse and keyboard as well as touch controls continue to roll out for more games, with Police Simulator: Patrol Officers getting support for the former while South of Midnight has gained the latter.
    • https://www.neowin.net/news/a-...erating-online-chat-client/ It looks like they're trying to reinvent the abaonded wheel.
  • Recent Achievements

    • Week One Done
      NeoWeen earned a badge
      Week One Done
    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
    • First Post
      Doreen768 earned a badge
      First Post
    • One Month Later
      James_kobe earned a badge
      One Month Later
    • Week One Done
      James_kobe earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      656
    2. 2
      ATLien_0
      255
    3. 3
      Xenon
      166
    4. 4
      neufuse
      146
    5. 5
      +FloatingFatMan
      121
  • Tell a friend

    Love Neowin? Tell a friend!