473,383 Members | 1,846 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,383 software developers and data experts.

ie gives a blank page with the below script, but fire fox works fine

Hi All
i am trying to change the font of my web page with user preference.i made use of cookie to get the user's preference, so that the font size selected will remain for the next session. i am changing the style sheet with the cookie set, The below shown is my code


Expand|Select|Wrap|Line Numbers
  1. function readCookie(name) {
  2.         alert("i am Reading the cookies");
  3.         var nameEQ = name + "=";
  4.         var ca = document.cookie.split(';');
  5.  
  6.         for(var i=0;i < ca.length;i++) {
  7.               var c = ca[i];
  8.              while (c.charAt(0)==' ')
  9.                 {
  10.                 c = c.substring(1,c.length);
  11.                 if (c.indexOf(nameEQ) == 0)
  12.                 return c.substring(nameEQ.length,c.length);
  13.                }
  14.  
  15. }
  16. return null;
  17. }
  18.  
  19.  
  20. function setFont(c_value)
  21. {
  22.  
  23. switch(c_value)
  24. {
  25. case 'big':
  26.  
  27. var navNam = navigator.appName;
  28. var navVer= parseFloat(navigator.appVersion);
  29.  
  30. if (navNam.indexOf('Microsoft') != -1 ) {
  31.         //IE 
  32.         document.write('<link rel="stylesheet" type="text/css" href="/imagesvariables_fonts_main_ie_big.css" />');
  33. }    else {
  34.         //Netscape
  35.         document.write('<link rel="stylesheet" type="text/css" href="/images/variables_fonts_main_net_big.css" />');
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42. break;
  43. case 'small':
  44. var navNam = navigator.appName;
  45. var navVer= parseFloat(navigator.appVersion);
  46.  
  47.  
  48. if (navNam.indexOf('Microsoft') != -1 ) {
  49.         //IE 
  50.         document.write('<link rel="stylesheet" type="text/css" href="/images/variables_fonts_main_ie.css" />');
  51. }    else {
  52.         //Netscape
  53.         document.write('<link rel="stylesheet" type="text/css" href="/images/variables_fonts_main_ie.css" />');
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60. break;
  61.  
  62.  
  63. }
  64.  
  65. }
  66.  
  67. function toggleFont()
  68. {
  69. var value;
  70. value=readCookie("size");
  71. var name;
  72. var c_value;
  73.  
  74.  
  75. switch(value)
  76. {
  77.  
  78. case 'big':
  79. alert("i am inside big");
  80. document.cookie ='size=small; expires=Fri, 3 Aug 2020 20:47:11 UTC; path=/';
  81. setFont("small");
  82. break;
  83.  
  84.  case 'small':
  85.  alert("i am inside small");
  86.   document.cookie ='size=big; expires=Fri, 3 Aug 2020 20:47:11 UTC; path=/';
  87.   setFont("big");
  88.   break;
  89.  
  90. default:
  91. alert("cookie not found!");
  92. document.cookie ='size=small; expires=Fri, 3 Aug 2020 20:47:11 UTC; path=/';
  93.  
  94.  
  95. }
  96.  
  97. }    
[HTML] //-->
</script>
<a class=pagenav href="#" onClick="javascript:toggleFont();" >Text size</a></span>
[/HTML]


but the above code works in fire fox, but it is gives a blank page in ie,can any one help me with the issue?
Aug 3 '08 #1
13 2227
when the user clicks on the Text size link it will call the Toggle font function, that will read the cookie value using readCookie function, if the cookie value set is small it will change it to big and the the call the setFont function,

My problem occurs in the setFont function, it goes inside the proper case of the set Font function, after that it gives a blank page in ie, but in fire fox it works fine,

Any of your advise would be much appreciated.
Aug 3 '08 #2
Even i can see the "downloading css file " info in the ie , but after that it gives the blank page.
Aug 3 '08 #3
acoder
16,027 Expert Mod 8TB
You can't use document.write after the page has loaded. It will open the document again for writing which is not what you want. For using alternate stylesheets with cookies, see this link.
Aug 3 '08 #4
acoder
16,027 Expert Mod 8TB
Two more quick points:

If all you want is to change a couple of style rules, use elem.style.color instead of changing the whole stylesheet.

Don't use browser detection. There's no need for it here.
Aug 3 '08 #5
Hi the link which you have given is really very useful.Timely help!!!!!!!!

i am working on it. i will send you the feed back soon. But i could not under
stand your second reply mail. Could you please elaborate that
Aug 4 '08 #6
acoder
16,027 Expert Mod 8TB
Sorry, elem.style.color should have been elem.style.fontSize. If you only need to change a few elements, then use JavaScript to change the font size - see link. This is a lot better than changing the whole stylesheet. If, however, there a quite a number of changes, it might be better to change the stylesheet. Good design helps here.

Secondly, browser detection (e.g. using navigator.appName) is often misused. It is rarely needed. Use feature detection instead- see this link for a good explanation.
Aug 4 '08 #7
i have changed my code to as shown below:

but still it is not working.Help me if you see any bug in it,
[HTML]<link rel="stylesheet" type="text/css" rel="stylesheet" title="big" href="/images/variables_fonts_main_ie_big.css"/>

<link rel="stylesheet" type="text/css" rel="stylesheet" title="small" href="/images/variables_fonts_main_ie.css"/>



<script type="text/JavaScript">




function readCookie(name) {
alert("i am Reading the cookies");
var nameEQ = name + "=";
alert(nameEQ);
var ca = document.cookie.split(';');

for(var i=0;i < ca.length;i++) {

var c = ca[i];


while (c.charAt(0)==' ')
{
c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0)
return c.substring(nameEQ.length,c.length);
}

}
return null;
}



function setFont(c_value)
{
var i, a, main;

switch(c_value)
{
case 'big':

alert("i am inside big");

for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
{
alert(" i am inside for loop");
if(a.getAttribute("rel").indexOf("style") != -1&&a.getAttribute("title"))
{
alert ("i am inside the if")

a.disabled = true;

if(a.getAttribute("title") == c_value)
{

alert(title);
a.disabled = false;

}

}

}




break;
case 'small':
document.cookie ='size=big; expires=Fri, 3 Aug 2020 20:47:11 UTC; path=/'

alert("i am inside small");

for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
{
alert("i am inside the for loop");

if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("c_value"))
{
a.disabled = true;

alert(c_value);

if(a.getAttribute("title") == c_value)

alert(c_value);

a.disabled = false;
}
}

break;


default:

alert("Cookie not found!");

document.cookie ='size=big; expires=Fri, 3 Aug 2020 20:47:11 UTC; path=/'




}

}




var size;
var value;
var navNam = navigator.appName;
var navVer= parseFloat(navigator.appVersion);
value=readCookie("size");
alert("value of font_size cookie is"+value);
setFont(value);

</script>[/HTML]
Aug 4 '08 #8
cookie value and style sheet title are not equal at any time, i don understand how it happens, and sorry for taking more space !
Aug 4 '08 #9
acoder
16,027 Expert Mod 8TB
Did you follow the steps laid out in the link properly?

I notice you have duplicate rel attributes.

What happens with the code you've posted? Do you get any errors?
Aug 4 '08 #10
Thanks for the reply acoder, i have used duplicate rel attribute. after seeing your advice, i have removed them after that the code works fine with ie, to have different style sheets for ie and Fire fox i have read the link given here.


Thanks a lot for help...

and i have one more requirement, Is it possible to swap image with the help of style sheets ? if i have style sheet a1, i should use image a1, if i have style sheet a2 i should use image a2. Is it possibel to do it?
Aug 5 '08 #11
acoder
16,027 Expert Mod 8TB
Yes, it should be possible. The best way would be to try it.
Aug 5 '08 #12
i have achived image swapping with the below code
Expand|Select|Wrap|Line Numbers
  1. <img  name="picture" src="icon1.gif" onClick="swapimage();"></a>
  2.  
  3. swapimage()
  4. {
  5. window.document.picture.src="icon2.gif";
  6.  
  7. }
  8.  
This is a very small tip, but hope it would be useful for new joinees of the javascript world
Aug 9 '08 #13
acoder
16,027 Expert Mod 8TB
I thought you were going to use stylesheets.

In your script, you should try to use IDs to refer to elements. See this simple example
Aug 9 '08 #14

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

Similar topics

2
by: Trent | last post by:
Hey All, I am new to javascript and currently coding a site with scripts that are beyond my level of understanding. The problematic page has thumbnail images that can be clicked on to zoom in....
6
by: Tony K | last post by:
I have the most peculiar problem with an ASP.NET page which we use for downloading a file. When the user clicks on a link, the link points to an ASPX page which downloads the file selected. ...
1
by: Tebogo Tefo via .NET 247 | last post by:
Hi I have a user control that contains three dropdownlists that I populate according to what was selected in the other dropdownlist (i.e. populate dropdownlist2 after selecting a value in...
9
by: tshad | last post by:
This is from my previous post, but a different issue. I have the following Javascript routine that opens a popup page, but doesn't seem to work if called from an asp.net button. It seems to work...
13
by: kurtj | last post by:
Hello Gurus: I have a validation script (below) that is somehow messed up. If the Name field is blank, I get the alert message, then the browser window goes to a blank document with the word...
6
by: massic80 | last post by:
Hi, it's me again! I have a strange problem with a form I gotta (dynamically) insert in a site, and copied/pasted the code to a blank page, to make it easier to try it out. I was using the $...
0
by: manywolf | last post by:
I have an aspx page that fires the page load event twice for every load. I tried every fix that was suggested in all the posts on this and other forums. None changed the behavior. After one post that...
13
by: raamay | last post by:
i have the following code which is generated from a .dwt template. My problem is that the page loads but only the header part displays and rest of the contents are not displayed. Everything works...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.