944,183 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1667
  • Java RSS
Aug 25th, 2006
0

Generating dynamic text sizes us JS

Expand Post »
THis i sprobably a fairly simple question to answer, but i dont know much about writing in javascript. IM trying to design a webpage in which the h1 tag will dynamicly size so that it spans across the width of the page. I know this can't be done without javascript because every browser and resollution renders text differently. Any hints on how i could go about doing this?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
sandbox is offline Offline
37 posts
since May 2006
Aug 25th, 2006
0

Re: Generating dynamic text sizes us JS

You could most likely use CSS.
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Aug 25th, 2006
0

Re: Generating dynamic text sizes us JS

No. You can't. Well, not completely. There are several CSS hacks that i could use to accomplish this. However, hacks don't work for every browser and this is were my problem lies. I'm trying to make something universal and i know javascript is pretty much a universal language for most browsers
Reputation Points: 10
Solved Threads: 1
Light Poster
sandbox is offline Offline
37 posts
since May 2006
Aug 25th, 2006
0

Re: Generating dynamic text sizes us JS

ah shoot... when i rad this sections title.. i saw "java" when i am looking for javascript. sorry guys =)
Reputation Points: 10
Solved Threads: 1
Light Poster
sandbox is offline Offline
37 posts
since May 2006
Sep 1st, 2006
0

Re: Generating dynamic text sizes us JS

I have no idea why you'd wanna do this! and yeah, Java != JavaScript, although clearly some copying went on, on more levels than just name... JavaScript is NOT the same on all browsers though.

The code below is tested in Firefox and IE - it's a bit hacky to get the window width on a browser that doesnt define the window.innerWidth property by using an absolutely placed div element and measuring it.. but it works nonetheless.

Be careful what you put in the h1 tag; if it's short, then it'll take up most of the vertical page estate at reasonable page sizes, don't put page breaks in it either .

This only affects one id-ed element. "factor" is an arbitrary value, 2 is probably a little too big...

Java Syntax (Toggle Plain Text)
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Untitled Document</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <script language="JavaScript">
  7. <!--
  8. var factor = 2;
  9. function adjustSize(txt){
  10. var target = document.getElementById(txt);
  11. var charCount = target.innerHTML.length;
  12. var docWidth;
  13. if(window.innerWidth){
  14. docWidth = window.innerWidth;
  15. }else{
  16. docWidth = document.getElementById('ruler').style.pixelWidth;
  17. }
  18. target.style.fontSize = (docWidth * factor)/charCount + "px";
  19. }
  20. -->
  21. </script>
  22. </head>
  23. <body onResize="adjustSize('header')" onLoad="adjustSize('header')">
  24. <h1 id="header">hello!</h1>
  25. <div id="ruler" style="position: absolute; width: 100%"></div>
  26. </body>
  27. </html>
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: join thread
Next Thread in Java Forum Timeline: Data structure + GUI question.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC