Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6709337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:52:08+00:00 2026-05-26T07:52:08+00:00

I am trying to run the following code, to assign user selected text in

  • 0

I am trying to run the following code, to assign user selected text in web page to a variable.

The code works fine if the web page(in which text has been selected) is within the main window, however if the text is in an iframe, then the code is unable to detect the iframe at all.

Javascript code–

var iframe_or_main= 'main';
var selection;
selection = document.getSelection();
if (!selection) //this means that user has selected text within an iframe (and not within main window)...
{   
    //this variable tells us that the user has selected text within an iframe... 
    //this is required when adding an element with translated text to web page...
    iframe_or_main='iframe';
    var id_of_iframe;
    id_of_iframe= getIframeWithSelection(window);
    console.log(" Content selected within an iframe- iframe id=" + id_of_iframe);
    selection = getIFrameDocument(document.getElementById(id_of_iframe)).getSelection();


}
//  return; // selection is probably in an iframe;

console.log(" Indicator - iframe or main? --" + iframe_or_main)";
console.log(" Iframe id (if applicable)=" + id_of_iframe);

Also, output when I select text in an iframe, is shown below–

Indicator - iframe or main? --main
Iframe id (if applicable)=undefined

Note that I have tried selecting text in a web page (open in iframe) that belongs to same domain as main web page. I also tried doing this with same origin policy disabled in Google Chrome- using start parameters to disable it. In both cases I am getting the same output as above.

Is there something wrong with my code? How do I fix it?
Please note that I want the code to work in Google Chrome, although if it works across other browsers that would be a plus.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:52:09+00:00Added an answer on May 26, 2026 at 7:52 am

    You cannot access the DOM of a document by using the IFrame as the root. Use the .contentWindow property to access the window of an iframe. Also, about “ID of frame, if applicable”. Your code will break when the IFrame doesn’t have an ID. See the code proposal at the bottom of this answer.

    if (!selection){   
        iframe_or_main = 'iframe';
        var iframes = document.getElementsByTagName("iframe");
        var test_selection;
        for(var i=0; i<iframes.length; i++){
            try{ //Try-catch to prevent the same-origin policy from breaking the code
                if(test_selection = iframes[i].contentWindow.document.getSelection()){
                    id_of_frame = iframes[i].id;
                    selection = test_selection;
                    break;
                }
            }catch(e){}
        }
        console.log(" Content selected within an iframe- iframe id=" + id_of_iframe);    
    }
    

    If the ID of the frame doesn’t matter, use the following code:

    if (!selection){   
        iframe_or_main = 'iframe';
        var test_selection;
        for(var i=0; i<frames.length; i++){
            try{ //Try-catch to prevent the same-origin policy from breaking the code
                if(test_selection = frames[i].document.getSelection()){
                    selection = test_selection;
                    break;
                }
            }catch(e){}
        } 
    }
    

    See also: determine the frame id/name when a user has selected text in that frame- the page has multiple frames

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run the following code (which I got from here ).
I am trying to run the following code : classA = classD; to assign
I'm trying to run the following code in python3, but it has been written
I am trying to run the following code from within Eclipse: Process process =
I'm using the following code to run a passthrough query. I'm trying to run
I am trying to run the following code: public BigDecimal valuate(String searchTerms, String categoryPath)
I'm trying to run the following code with Ninject.Moq: [TestMethod] public void TestMethod1() {
I am trying to run the following ruby code from IRB but am unable
I'm trying to run this jQuery selector: $(#label option[value=\newLabel\]) On the following code: <select
The ReportInfo is a structure. The structure works fine on one web page but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.