By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,254 Members | 1,713 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,254 IT Pros & Developers. It's quick & easy.

Object Expected Error

P: 14
HELLO, I AM VERY NEW TO JAVA SCRIPT PROGRAMMING.
I AM PUTTING TOGETHER A PAGE FOR A FRIEND. THAT LOOK DEPENDS VERY MUCH ON SCREEN RESOLUTION, SO I AM TRY TO DETECT IT, AND CHANGE FONTS ACCORDINGLY. MY CODE SO FAR IS:

Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <script type="text/javascript">
  3.  
  4. function chkwidth()
  5. {
  6.  alert(window.screen.width);
  7.  
  8.  x = window.screen.width;
  9.  
  10.  if x == 1280
  11.   {
  12.  
  13.    document.write("<style type='text/css'> <!-- .style1 {font-size: 18px} --> </style>");
  14.   }
  15.  
  16.  else
  17.  {
  18.    document.write("<style type='text/css'> <!-- .style1 {font-size: 10px} --> </style>");
  19.   }
  20. </script>
  21. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  22. <title>Untitled Document</title>
  23. </head>
  24. <body onload="chkwidth()">
  25. <span class="style1">test text</span>
  26. </body>
  27. </html>
  28.  
HOWEVER, WHEN I RUN THE PAGE, I ALWAYS GET THE ERROR OBJECT EXPECTED. WHEN I RUN THE FUNCTION WITH JUST 'ALERT(WINDOW.SCREEN.WIDTH) IT WORKS FINE, SO I CAN'T UNDERSTAND WHERE THE PROBLEM IS.

CAN ANYONE HELP?
Jun 30 '08 #1
Share this Question
Share on Google+
12 Replies


Markus
Expert 5K+
P: 6,050
Shouldn't the if be in parenthesis? i.e. if(statement)

Also, why are you typing in caps?
Jun 30 '08 #2

P: 14
am typing in caps to differentiate between me and the code, sorry if it seems i'm shouting.

the if statement is in the curly brackets of the function.

how would you write it?
Jun 30 '08 #3

P: 14
sorry, see what you mean, should be if(condition)....

anyway, i don't get the error any more, but the style font is not being set, i.e

document.write("<style type='text/css'> <!-- .style1 {font-size: 18px} --> </style>");

is not being written to page. i know this because the text 'hello' is not being set the sz 18.

any ideas
Jun 30 '08 #4

acoder
Expert Mod 15k+
P: 16,027
am typing in caps to differentiate between me and the code, sorry if it seems i'm shouting.
To differentiate between the text and the code, use code tags - see How to Ask a Question.

PS. welcome to Bytes!
Jun 30 '08 #5

P: 14
thanks

any idea on how to create my style with javascript?
Jun 30 '08 #6

acoder
Expert Mod 15k+
P: 16,027
To change styles using JavaScript, use the Style object. JavaScript uses camel case, so font-size would be fontSize.
Jun 30 '08 #7

P: 14
sorry to sound thick, but how would i implement this? at the moment, it's a very basic page, with no div's. so my code is:

Expand|Select|Wrap|Line Numbers
  1. function chkwidth()
  2. {
  3.  
  4.  
  5.  if (window.screen.width == 1280)
  6.   {
  7.      document.getElementById("style1").style.text.fontSize="28";
  8.   }
  9.  
  10.  
  11. ....
  12.  
  13. <body onload="chkwidth()">
  14.  
  15. <span class="style1">test text</span>
  16.  
  17. </body>
as i say, i'm very new to this, so what should my code look like?
Jun 30 '08 #8

acoder
Expert Mod 15k+
P: 16,027
document.getElementById() accesses an element by its ID, so your span or div should have an id of "style1", e.g.
Expand|Select|Wrap|Line Numbers
  1. <div id="style1">...</div>
It should be style.fontSize, not style.text.fontSize. See this link on how to use style.fontSize.

One other point to note: what if the screen is wider than 1280?

Finally, you shouldn't need to adjust the font with screen size. The user could do it themselves using the browser menu or via keyboard shortcuts as long you use relative measurements, e.g. "em", "px", etc. You could provide this in the browser using links/buttons.
Jun 30 '08 #9

P: 14
i've done all my measurements using percentages rather than pixels.
but i find that when viewing in lower resolutions, font sizes, if not reset, looks dispraportionate.

i've put my the 1280 check in to start with, but will run checks for other widths.

using the code i've already given you, what do you think the best way to write it is?

also, to clarify, what exactly is wrong with my code i have written in the previous post?
Jun 30 '08 #10

acoder
Expert Mod 15k+
P: 16,027
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("style1").style.text.fontSize="28";
should be
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("style1").style.fontSize="28px";
and
Expand|Select|Wrap|Line Numbers
  1. <span class="style1">test text</span>
should be
Expand|Select|Wrap|Line Numbers
  1. <span id="style1">test text</span>
Jun 30 '08 #11

P: 14
thanks, works fine. hope to speak again
Jun 30 '08 #12

acoder
Expert Mod 15k+
P: 16,027
No problem. Since you're new to JavaScript, why not take a look at this tutorial to get you up and running.
Jul 1 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.