0

I am trying to use Javascript to open a new document from an old one, ideally in the same window, and change an innerHTML in the latter.

If necessary, I could go with the second document being opened in a different window.

I'm guessing this cannot be done...

khargushoghli
  • 55
  • 1
  • 1
  • 12
  • First you say you want to open a new window and then you say ideally a new window will not be opened, could you try to explain in more detail what you want? – Markus-ipse Jul 07 '13 at 20:47
  • Oops. I edited it so that it would make sense. Thanks. – khargushoghli Jul 07 '13 at 21:13
  • so in your page you want to open another page? If I understand correctly it seems like you want to use an iframe – Markus-ipse Jul 07 '13 at 21:29
  • Thanks. I'm sorry, I'm not making myself clear. A document lies in a window. I want to open a new document in that same window. You know, using _self. – khargushoghli Jul 08 '13 at 02:30
  • by document you mean another html page? and you want to replace the old page entirely? – Markus-ipse Jul 08 '13 at 19:29
  • Yes. The point is, I have a book in 15 chapters. There are references from one part of the book to another part of the book. I want the reader to be able to get to the referred to passage, find it easily (so it is somehow marked in the second page, but I don't want the mark to be in the HTML because it would disturb the reader who is simply reading the passage in the flow of the text, and not because it is referred to. The mark will be in the form of a visible link invoked by the first page as a change of the innerHTML of a link. On clicking the link, the visible link disappears. – khargushoghli Jul 08 '13 at 23:26
  • It would not kill me to open a second window with the second page in it, if that is necessary. I just don't want a proliferation of opening windows. – khargushoghli Jul 08 '13 at 23:27

1 Answers1

0

you could add a query string parameter to your links pointing to other parts of the book so that instead of

<a href="page5.html">reference to page 5</a> 

you would have

<a href="page5.html?reference=ref1">reference to page 5</a>

and on page5.html you can highlight the reference with a span tag

Lorem ipsum dolor sit amet, <span id="ref1">consectetur adipiscing elit</span>. 

and add script similar to this

var refToHighlight = getQueryString("reference"); //you will have to write your own function to get qs data

//removes the span and replaces it with a link
$('#' + refToHighlight).replace('<a class="reference" href="#"/>');

//changes the link back to span when clicked
$('.reference').click(function(){
    $(this).contents().replace('<span/>');
});

This way when a user browses regularly to page5 (clicking links not ending in ?reference=ref1) the reference will not be highlighted as its marked with a span, but on the other hand if the user clicks a link containing the query parameter the span will be changed to a link that will highlight the reference now.

Markus-ipse
  • 7,196
  • 4
  • 29
  • 34
  • Sorry. Been working on my own projects and haven't had a chance to look at this. Embarrassed to say that I don't know enough jQuery to even write the function to get the qs data... – khargushoghli Aug 23 '13 at 16:23
  • you can check the below SO question for details on how to get query string data http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values – Markus-ipse Aug 24 '13 at 10:43