views:

27

answers:

1

I have a JavaScript file highlight.js which highlights all strings within a given document class with a certain background color. eg.

$("div").highlight("the")

This highlights all occurrences of "the" within all divs with a yellow background.

I am writing a web application; which needs to open an link to an HTML page and automatically execute this JavaScript on that webpage. How can this be done?

A: 

Unfortunately, for security reasons, browsers disallow the modification of the DOM on another page. If you owned the code in the target page too, however, you could add the highlight code to that target page, and pass the word to be highlighted as a hashtag or query string, for example:

  • main page opens targetPage?highlight=the
  • targetPage has code to parse the query string and grab the highlight word
  • targetPage has the highlight code which then highlights the word
Delan Azabani