SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Get iframe name

  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get iframe name

    Hi I'm a newbie at javascript and I was wondering how can I extract the iframe name using regex only? so for example if the string value contains:
    Code:
    <DIV style="MARGIN-TOP: 0px; WIDTH: 670px; HEIGHT: 210px; VISIBILITY: visible; MARGIN-LEFT: -335px; TOP: 48px" id=TB_window><DIV id=TB_title>  <DIV id=TB_ajaxWindowTitle>Add Media</DIV>  <DIV id=TB_closeAjaxWindow><A id=TB_closeWindowButton title=Close href="#" jQuery172014112867239284427="140"><IMG src="http://www.gorgeoushentai.com/wp-includes/js/thickbox/tb-close.png"></A></DIV></DIV><IFRAME style="WIDTH: 670px; HEIGHT: 180px" id=TB_iframeContent onload=tb_showIframe() src="http://www.gorgeoushentai.com/wp-admin/media-upload.php?post_id=168&" frameBorder=0 name=TB_iframeContent656 hspace=0>This feature requires inline frames. You have iframes disabled or your browser does not support them.</IFRAME></DIV>
    then it would extract TB_iframeContent656

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    You could do it like this:

    Code JavaScript:
    var string = '<DIV style="MARGIN-TOP: 0px; WIDTH: 670px; HEIGHT: 210px; VISIBILITY: visible; MARGIN-LEFT: -335px; TOP: 48px" id=TB_window><DIV id=TB_title>  <DIV id=TB_ajaxWindowTitle>Add Media</DIV>  <DIV id=TB_closeAjaxWindow><A id=TB_closeWindowButton title=Close href="#" jQuery172014112867239284427="140"><IMG src="http://www.gorgeoushentai.com/wp-includes/js/thickbox/tb-close.png"></A></DIV></DIV><IFRAME style="WIDTH: 670px; HEIGHT: 180px" id=TB_iframeContent onload=tb_showIframe() src="http://www.gorgeoushentai.com/wp-admin/media-upload.php?post_id=168&" frameBorder=0 name=TB_iframeContent656 hspace=0>This feature requires inline frames. You have iframes disabled or your browser does not support them.</IFRAME></DIV>';
    var regex = /<IFRAME.*name=(.+?) .*<\/IFRAME>/;
    var m = string.match(regex);
    alert(m[1]);

    The above regular expression tries to match :
    '<IFRAME'
    any amount of characters (greedy)
    'name='
    any amount of characters (non-greedy)
    a space
    any amount of characters (greedy)
    '</IFRAME>'

    I have wrapped what comes after 'name=' in parentheses, so that it is available as in the matches array (m[1]).

    Although this works, it is very brittle and might not be the best way to accomplish what you want.
    If you only have one iFrame on the page for example, you could instead try:
    Code JavaScript:
    alert(document.getElementsByTagName('iframe')[0].name);
    Or, if you can reference it by name:
    Code JavaScript:
    alert(document.getElementById("TB_iframeContent").name);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •