473,382 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Object Expected Error

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
12 1045
Markus
6,050 Expert 4TB
Shouldn't the if be in parenthesis? i.e. if(statement)

Also, why are you typing in caps?
Jun 30 '08 #2
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
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
16,027 Expert Mod 8TB
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
thanks

any idea on how to create my style with javascript?
Jun 30 '08 #6
acoder
16,027 Expert Mod 8TB
To change styles using JavaScript, use the Style object. JavaScript uses camel case, so font-size would be fontSize.
Jun 30 '08 #7
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
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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
thanks, works fine. hope to speak again
Jun 30 '08 #12
acoder
16,027 Expert Mod 8TB
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

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

Similar topics

4
by: Bill | last post by:
I call a function in my .js file like this: onClick="location.href='blank.html' + generateSearchStringFromForm('section')" where section is the name of my form. The function is defined as...
2
by: jsnX | last post by:
i want a function object that is a) initialized with an STL container foo b) will search foo for an object of type foo::value_type here is my code: ...
11
by: westplastic | last post by:
This one is driving me insane. The script works perfect on Firefox, but Internet Explorer keeps complaining about "Error Object Expected" and stuff like that. I've run it through Firefox's Java...
1
by: JOJO123 | last post by:
I got here in search of an answer to this Javascrpt question. I upgraded jave on XP Ie 7, acrobat 5.1 and suddenly can't open any pdf files on web sites using IE. I see u guys all say, this is a...
2
by: thj | last post by:
Hi. I've got this form that I'm trying to validate: <form id="periodForm" action="" method="post"> <p> Periode: <input id="startDate" name="startDate" type="text" size="7" value="<%=...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.