Copying styles from one window to another...

Reply

Join Date: Dec 2006
Posts: 14
Reputation: evank is an unknown quantity at this point 
Solved Threads: 0
evank's Avatar
evank evank is offline Offline
Newbie Poster

Copying styles from one window to another...

 
0
  #1
May 4th, 2007
I have a script that opens up a blank popup window, then writes some content to it, like so:

HTML and CSS Syntax (Toggle Plain Text)
  1. var popup = window.open('', 'window_id');
  2. popup.document.open("text/html","replace");
  3. popup.document.writeln("some content");
  4. popup.document.title = 'page title';
  5. popup.document.close();
Now, in this newly opened popup window, I want to duplicate any stylesheets from the parent window. I expected this to be straightforward:

HTML and CSS Syntax (Toggle Plain Text)
  1. popup.document.styleSheets = window.document.styleSheets;
Unfortunately, that doesn't work because the styleSheets attribute seems to be read-only...I don't care about inline styles, just <style> and <link ref="stylesheet"> tags, so I tried using document.getElementsByTagName(), but can't find a cross-browser way to reliably rebuild said tags.

I've been banging my head for a few hours now and was hoping someone here could offer advice. Is there a simple way to do this?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,210
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Copying styles from one window to another...

 
0
  #2
May 5th, 2007
I can see that you have to have an external stylesheet. Then you can use:
HTML and CSS Syntax (Toggle Plain Text)
  1. popup.document.write('<html>');
  2. popup.document.write('<head>');
  3. popup.document.write('<title>my popup</title>');
  4. popup.document.write('<link rel="stylesheet" type="text/css" href="mystylesheet.css" />');
  5. popup.document.write('</head>');
  6. .....
Each document.write is a line of html code.
Last edited by MidiMagic; May 5th, 2007 at 12:52 am.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 14
Reputation: evank is an unknown quantity at this point 
Solved Threads: 0
evank's Avatar
evank evank is offline Offline
Newbie Poster

Re: Copying styles from one window to another...

 
0
  #3
May 5th, 2007
Sorry, I guess I wasnt clear enough. It's not as simple as a document write, since I don't know what stylesheets the page is using ahead of time.

I need to programatically find any and all document-level styles (STYLE tags) and external stylesheets (LINK tags) from the parent window and reproduce them in the popup window.

This will be used across many sites with many differing style implementations, hence the difficulty.
--
The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
~Terry Pratchett
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,210
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Copying styles from one window to another...

 
0
  #4
May 7th, 2007
I thought you would be using a common stylesheet for all of the pages. If you have control over all of these web pages, you can use a common stylesheet.

Are you trying to find out about stylesheets that other programmers wrote for their own pages?

If so, you are asking for, if not the impossible, the very difficult. It's effectively reverse-engineering the other programmer's page. It's a Brobdingnagian task.

You can't know in advance what the other programmer named his styles, or what he intended to use them for.

You CAN find out the styles applied to a particular web page element, once you know the address index of that element. Use the style methods in JavaScript to find out the style values of the element in question.

e.g. This gets the page's main background color.

[code]
bgco = document.body.backgroundColor;
bgcolor = parseFloat(bgco);
[/quote]

Be aware that this works on only those browsers which use the W3C DOM. Ift fails of an older browser is used to display the page.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 14
Reputation: evank is an unknown quantity at this point 
Solved Threads: 0
evank's Avatar
evank evank is offline Offline
Newbie Poster

Re: Copying styles from one window to another...

 
0
  #5
May 7th, 2007
Best practices aside, I don't have control of the target pages, which is why I can't hardcode styles into the javascript, and why I want to duplicate them dynamically and programmatically.


It's easy enough to grab the tags themselves as DOM objects:
HTML and CSS Syntax (Toggle Plain Text)
  1. var styleTags = document.getElementsByTagName('style');
  2. var linkTags = document.getElementsByTagName('link');
And still easy to remove any link tags that aren't style-sheets:
HTML and CSS Syntax (Toggle Plain Text)
  1. var temp = Array();
  2. for(i = 0; i < linkTags.length; i++) {
  3. if(linkTags[i].getAttribute('rel').toLowerCase() == 'stylesheet') {
  4. temp[temp.length] = linkTags[i];
  5. }
  6. }
  7. linkTags = temp;
What I'm having difficulty in, is reconstructing these elements. Specifically, how do I reconstruct a style tag, since I'm not sure how to get the contents of it? (innerHTML, innerText, and outerHTML don't work.)
--
The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
~Terry Pratchett
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,210
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: Copying styles from one window to another...

 
0
  #6
May 7th, 2007
The innerHTML, innerText, and outerHTML don't work because style sheets aren't html code.

My question is, what are you going to do with the style elements if you do reconstruct them? They won't fit your pages, because you don't have the same class names.

Actually, a larger question arises: Why do you need it? Maybe if I understood what you are really trying to do with this information, I can find a better way.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: Copying styles from one window to another...

 
0
  #7
May 7th, 2007
Originally Posted by evank
innerHTML, innerText, and outerHTML don't work.
innerHTML certainly works. Providing that you have a valid reference to the <style> element that you want to extract; innerHTML will return the 'body' of the style; that is, any rules in the style, exactly as is is in the <style> itself. This behaviour was tested using code following this post, in Opera 9, Firefox 2, and even Internet Explorer 5**.

** EDIT: Sorry; that's Internet Explorer 6 not Internet Explorer 5...

For <link> elements, look at the 'href' attribute of a reference to any <link>, targetting only 'stylesheet' ones obviously. Some browsers (Opera 9, Firefox 2) will give you an absolute URL to the stylesheet; others may not be as helpful (IE5** just gives the href as specified in the link href). Depending on what you want to DO with the style information, will determine what to do with the href. In my example code below, I could use the href in one of my own <links>, and it would be like making any other (remote) stylesheet link. IF you wanted to read the content at a href; you might hit problems, specifically, Javascript wont permit you to download and inspect information from a domain other than that on which the script is a resident. The only way to get around that ( unless there IS a way speficially for stylesheets) , would be to use some kind of interim server side code; perhaps an XMLHTTP request asking your own server to download the CSS document and return the result to Javascript.

Which leads onto; why do anything like this in Javascript? You might want to try using something like Perl to download and dissect pages on your server; it would not be an enourmas task.

The pasted code is a contrived example; obviously, I wrote the code, so I don't need to go searching for the <link> and <style>, hence ids.

Since you say you can already get references to the objects that you need, I can't see a problem. Of course, I don't know what you're trying to do, but it shouldn't be too difficult to take the information returned from style.innerHTML or link.href; and put it into new objects on dynamic pages; this is also done in the given example, using a hacky window.document.write approach.

As MidiMagic said; if you have no control over these pages, you wont know which elements the CSS styles relate to ( and in what order ), or what Id-ing/classing policies these developers are using, etc.

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html>
  3. <head>
  4. <link rel="stylsheet" type="text/css" href="mystyle.css" id="linked"/>
  5. <style type="text/css" id="inline">
  6. body{
  7. font-family:Arial, sans-serif;
  8. font-weight:bold;
  9. }
  10. </style>
  11. <script type="text/javascript">
  12. function ripInlineStyle()
  13. {
  14. alert(document.getElementById("inline").innerHTML);
  15. }
  16. function ripLinkedStyle()
  17. {
  18. alert(document.getElementById("linked").href);
  19. }
  20. function ripAndDisplay()
  21. {
  22. var inline_style = document.getElementById("inline").innerHTML;
  23. var targ = window.open( );
  24. targ.document.write( "<html><head><style type='text/css'>" );
  25. targ.document.write( inline_style );
  26. targ.document.write( "</style></head><body>Took inline style:");
  27. targ.document.write( inline_style );
  28. targ.document.write( "<hr/> Could also have linked to:" );
  29. targ.document.write( document.getElementById("linked").href );
  30. targ.document.write("</body></html>" );
  31. }
  32. </script>
  33. </head>
  34. <body onload="ripAndDisplay();">
  35. Page will launch a popup. Allow popup.
  36. </body>
  37. </html>
Last edited by MattEvans; May 7th, 2007 at 6:57 pm.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the HTML and CSS Forum


Views: 2800 | Replies: 6
Thread Tools Search this Thread



Tag cloud for HTML and CSS
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC