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: - <head>
-
<script type="text/javascript">
-
-
function chkwidth()
-
{
-
alert(window.screen.width);
-
-
x = window.screen.width;
-
-
if x == 1280
-
{
-
-
document.write("<style type='text/css'> <!-- .style1 {font-size: 18px} --> </style>");
-
}
-
-
else
-
{
-
document.write("<style type='text/css'> <!-- .style1 {font-size: 10px} --> </style>");
-
}
-
}
-
</script>
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
<title>Untitled Document</title>
-
</head>
-
<body onload="chkwidth()">
-
<span class="style1">test text</span>
-
</body>
-
</html>
-
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?
12 1045
Shouldn't the if be in parenthesis? i.e. if(statement)
Also, why are you typing in caps?
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?
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
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!
thanks
any idea on how to create my style with javascript?
To change styles using JavaScript, use the Style object. JavaScript uses camel case, so font-size would be fontSize.
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: - function chkwidth()
-
{
-
-
-
if (window.screen.width == 1280)
-
{
-
document.getElementById("style1").style.text.fontSize="28";
-
}
-
-
-
}
-
....
-
-
<body onload="chkwidth()">
-
-
<span class="style1">test text</span>
-
-
</body>
as i say, i'm very new to this, so what should my code look like?
document.getElementById() accesses an element by its ID, so your span or div should have an id of "style1", e.g. - <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.
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?
- document.getElementById("style1").style.text.fontSize="28";
should be - document.getElementById("style1").style.fontSize="28px";
and - <span class="style1">test text</span>
should be - <span id="style1">test text</span>
thanks, works fine. hope to speak again
No problem. Since you're new to JavaScript, why not take a look at this tutorial to get you up and running.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:
...
|
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...
|
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...
|
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="<%=...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |