Advertisement

06.05.2008 at 12:29PM PDT, ID: 23461609
[x]
Attachment Details

stop refresh on javascript call to change css

Asked by vkimura2007 in JavaScript

Tags: Javascript, IE 7; firefox 2.x

I'm wondering how to change the CSS using Javascript without "refreshing" the page. The page doesn't actually refresh; however, the scroll bar moves back to the top of the page. I can change the CSS using Javascript from the following which was taken from http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_22005907.htm. Is there a way to code so the scroll bar doesn't take the visitor back to the top of the page?

<html>
<head>
<title>Change Font Size</title>
<script type=text/javascript>
function ChangeFontSize(amt) {
  var minSize = 8;
  var maxSize = 24;
  var currentSize = getClassProperty("KYBD_Button","fontSize");
  currentSize = currentSize.replace("pt","");
  var newSize = parseInt(currentSize) + amt;
  if (newSize >= minSize && newSize <= maxSize) {
    changeClassProperty("KYBD_Button","fontSize",newSize + "pt");
  }
}
 
// get the value of a property of a CSS Rule
function getClassProperty(sClassName,sProperty) {
  sClassName="."+sClassName;
  var sheets = document.styleSheets;
  var rules;
  var styleObj;
  for (i=0;i< sheets.length; i++) {
    rules=sheets[i].cssRules || sheets[i].rules;
    for (var j=0; j<rules.length; j++) {
      if (rules[j].selectorText && rules[j].selectorText==sClassName) {
        styleObj=rules[j].style;
        return styleObj[sProperty];
      }
    }
  }
}

// set the value of a property of a CSS Rule
function changeClassProperty(sClassName,sProperty,sValue) {
  sClassName="."+sClassName;
  var sheets = document.styleSheets;
  var rules;
  var styleObj;
  for (i=0;i< sheets.length; i++) {
    rules=sheets[i].cssRules || sheets[i].rules;
    for (var j=0; j<rules.length; j++) {
      if (rules[j].selectorText && rules[j].selectorText==sClassName) {
        styleObj=rules[j].style;
        styleObj[sProperty]=sValue;
        break;
      }
    }
  }
}

</script>
<style type=text/css>
.KYBD_Button {font-size:12pt;}
               .a{
                  width: 100px;
                  height: 50px;
                  border: 1px dotted #ccc;
                  margin: 2px;
                  background-color: #fff;
                  }
                 
                  .b{
                  width: 100px;
                  height: 50px;
                  border: 1px dotted #ccc;
                  margin: 2px;
                  background-color: #fff;
                  }
</style>
</head>
<body>
<input type=button value="Font -" onclick="ChangeFontSize(-1);" />
<input type=button value="Font +" onclick="ChangeFontSize(+1);" /><br />
<span class="KYBD_Button">This font should change size when the buttons are clicked</span><br />
This font should remain static.<br><br>

      <br />
      <div id="main">
            <div class="a"></div>
            <div class="b"></div>
            <div class="a"></div>
            <div class="b"></div>
            <div class="a"></div>
            <div class="b"></div>
            <div class="a"></div>
      </div>
     
<br />
<a href="#" onClick="changeClassProperty('a','width', '120px');">Widen All Divs with Class 'a'</a><br />
<a href="#" onClick="changeClassProperty('b','background', '#ccc');">Color All Divs with Class 'b'</a>

</body>
</html>

Start Free Trial
 
Loading Advertisement...
 
[+][-]06.05.2008 at 04:47PM PDT, ID: 21725009

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: JavaScript
Tags: Javascript, IE 7; firefox 2.x
Sign Up Now!
Solution Provided By: Hube02
Participating Experts: 1
Solution Grade: A
 
 
[+][-]06.05.2008 at 09:14PM PDT, ID: 21726171

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628