2

I have an object embed html element that is used to display a webpage selected by the user.
The embed declaration is quite simple:

<embed id='frame' src=file></embed>

Where file is a url formatted in this way: "http:// www. something .bla" ; the element is created when the user enters a link in a search box and chooses to load it.

What I have to do is:
- assuming that the embed contains something (which means that the user has loaded a link),
- detect if the user has selected some text inside it (as an example, if the embed was showing this same page, and the user decides to select (highlight) the question title + the first row of the question, I should detect it),
- get the selection (html included) (so, in this example,

How do I get the selected text inside an object embed using jquery?


I have an object embed html element that is used to display a webpage selected by the user.

including the html tags,
- and then store it somewhere for further uses.

It may be done in whatever scripting language, it doesn't matter, but Javascript or jQuery would be better.
What I tried for the moment, using jQuery or Javascript:
- binding the getSelection() methods to the embed object (the embed id is "frame" in this case) like this:

document.getElementById('frame').getSelection()

- or like this:

$('frame').getSelection()

- or this:

$('frame').on("select", function() {
    some code to perform action
});

But it doesn't work.
Any help is very appreciated :)

tenik
  • 250
  • 1
  • 4
  • 20
  • I really can't fully understand what you want, what you wait from get selection to return ?? – Zakaria Acharki Aug 12 '15 at 12:11
  • Also not entirely sure what you're asking for here... You have html() which will return the HTML contents of an element? http://api.jquery.com/html/ - is that what you're looking for? – Luke Twomey Aug 12 '15 at 12:15
  • What I want is: assuming you have a random page loaded, like facebook homepage, this same page, etc., if the user selects some parts of it and then uses a proper button, the selected html will be stored somewhere. What I can't do is extract the selected part from the embed. – tenik Aug 12 '15 at 12:26

1 Answers1

0

You should use an <IFRAME> instead, embedding of HTML content like that does not expose its window/document object to the host page, and does not work at all in FireFox.

Assuming the loaded URL is on the same domain as the page you can access its document object.

If the URL is on a different domain without CORS enabled, what you want is impossible with JS alone.

Community
  • 1
  • 1
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • And assuming that CORS is enabled? I would use an iframe, but it has a "bad" reputation, and considering that I'm doing this in a university project, my teacher would hate to see an iframe. – tenik Aug 12 '15 at 12:27
  • If CORS was enabled the page would need to implement code to allow you to communicate with it (`postMessage`) I.e. you would need control of it. (`` would still not work; even if it did surely the lack of FireFox support would make it unusable.) – Alex K. Aug 12 '15 at 12:36