+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 11

Thread: JS or CSS? onclick problem...

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    35

    JS or CSS? onclick problem...

    I have a menu (image based):
    Code:
    <ul>
                
                    <li class="inactive" id="lib"><a href="test-tour/java_only.html" target="tour5" onclick="document.getElementById('headimg').src='images/libertyplan.png';this.className='active';document.getElementById('brk').className='inactive';document.getElementById('arl').className='inactive';document.getElementById('fre').className='inactive';"><span>The Liberty</span></a></li>
                    <li class="inactive" id="brk"><a href="test-tour/java_only.html" target="tour5" onclick="document.getElementById('headimg').src='images/brooklynplan.png';this.className='active';document.getElementById('lib').className='inactive';document.getElementById('arl').className='inactive';document.getElementById('fre').className='inactive';"><span>The Brooklyn</span></a></li>
                    <li class="inactive" id="arl"><a href="test-tour/java_only.html" target="tour5" onclick="document.getElementById('headimg').src='images/arlingtonplan.png';this.className='active';document.getElementById('lib').className='inactive';document.getElementById('brk').className='inactive';document.getElementById('fre').className='inactive';"><span>The Arlington</span></a></li>
                    <li class="inactive" id="fre"><a href="test-tour/java_only.html" target="tour5" onclick="document.getElementById('headimg').src='images/freemontplan.png';this.className='active';document.getElementById('lib').className='inactive';document.getElementById('brk').className='inactive';document.getElementById('arl').className='inactive';"><span>The Freemont</span></a></li>
                </ul>
    The client doesn't want any page reloading. So each click loads different content into an iframe (currently using same src). At the same time change the class of each link button to identify which is active.
    I can get the active class to change, but I'm struggling with "resetting" the other buttons with the same onclick event back to an original 'inactive' class.

    The onclick event also loads a header image related to the frame content.

    What am I doing wrong? Or is there an easier way to do this? I'm not much of a javascripter.


  2. #2
    aka sionnach iandevlin's Avatar
    Join Date
    Aug 2008
    Location
    Cambridge, UK
    Posts
    2,043
    It might help if you post the rest of the code too, i.e. the div's etc. it is referring to and trying to change.

  3. #3
    likes chips. Scriptage's Avatar
    Join Date
    May 2008
    Location
    Yorkshire
    Posts
    2,760
    For a start you really don't want to use iframes, iframes are unsupported in strict html / xhtml (the document types you should be using for production code). You should use AJAX:


    Code:
    <!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>JS</title>
    <style type="text/css">
    .active{
    background-color: red;
    color: white;
    }
    .inactive{
    background-color: green;
    color: yellow;
    }
    #cframe{
    width: 500px;
    height: 500px;
    }
    </style>
    <script type="text/javascript">
    /* <![CDATA[ */
    function GetRequestObject(){
       var XMLHttp;
       /*@cc_on
        @if (@_jscript_version >= 5)
        try {
          XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {
            XMLHttp = false;
          }
        }
       @else
       XMLHttp = false;
       @end @*/
       if (!XMLHttp && typeof XMLHttpRequest != 'undefined') {
        try {
          XMLHttp = new XMLHttpRequest();
        } catch (e) {
          XMLHttp = false;
        }
       }
       return XMLHttp;
    }
     
    function Page(Source, HeaderFile, ID){
    if(!window['GPList']){
    window['GPList'] = new Array();
    }
    this.Source = Source;
    this.HeaderFile = HeaderFile;
    this.Self = document.getElementById(ID);
    this.Active = function(Bool){
       if(Bool){
       this.Self.className = 'active';
       this.Self.firstChild.className = 'active';
       document.getElementById('headimg').src = this.HeaderFile;
       Request = GetRequestObject();
       if(!Request){
       return true;
       }
       Request.open("GET", this.Source ,true);
     
       Request.onreadystatechange = function(){
       if(Request.readystate == 4){ // Successfully sent
     
       var File = Request.responseText.substring(Request.responseText.indexOf('<body>'), Request.responseText.indexOf('</body>')+7);    
     
       document.getElementById("content").innerHTML = File;
     
       }
       };
     
    Request.send(null);
     
       }else{
       this.Self.className = 'inactive';
       this.Self.firstChild.className = 'inactive';
     
       }
     
    }
    window['GPList'].push(this);
    return this;
    }
    function LoadPage(Obj){
    var ID = Obj.parentNode.id;
       for(var i=0; i<GPList.length; i++){
     
          if(GPList[i].Self.id == ID){
          GPList[i].Active(1);
          }else{
          GPList[i].Active(0);
          }
       }
    return false;
    }
    /* ]]> */
    </script>
    </head>
    <body>
    <div id="header">
       <img id="headimg" src="default.jpg" alt="Page Heading" />
    </div>
    <div id="links">
    <ul>
        <li class="inactive" id="lib">
          <a href="libertyplan.html" onclick="LoadPage(this); return false">The Liberty</a>
        </li>
        <li class="inactive" id="brk">
          <a href="brooklynplan.html" onclick="LoadPage(this); return false;">The Brooklyn</a>
        </li>
    </ul>
    </div>
    <div id="content">
    </div>
    <script type="text/javascript">
    new Page('libertyplan.html', 'images/libertyplan.png', 'lib');
    new Page('brooklynplan.html', 'images/brooklynplan.png', 'brk');
    </script>
    </body>
    </html>
    Last edited by Scriptage; 28-02-2009 at 11:29 AM. Reason: Added CDATA tag to Javascript code

  4. #4
    Junior Member
    Join Date
    May 2008
    Posts
    35

    That ajax is over my head...
    I'll see if I can figure it out a little before I implement it (don't like copy n pasting just for the sake of a feature without understanding, at least a little).

    here's the site in question:
    The Brooklyn

    I didn't know that iFrames weren't standard in strict - I'll have to decide whether to fall back, or change all together.

    FWIW
    (just got safari 4 beta, testing it out... seems ok so far, don't like the chrome-like tabs at the top.. (on MBP/Leopard).

  5. #5
    Junior Member
    Join Date
    May 2008
    Posts
    35
    just out of curiosity, can anybody pick out why, in theory anyway, what I have in the first post isn't working?

  6. #6
    likes chips. Scriptage's Avatar
    Join Date
    May 2008
    Location
    Yorkshire
    Posts
    2,760
    this.parentNode.className='active'

    Seriously though, using iframes is totally inaccessible, if you want I can modify the code I posted and check it for cross browser issues etc.

  7. #7
    Junior Member
    Join Date
    May 2008
    Posts
    35
    Hi Scriptage,

    Something's not working/(or me) with this script. Obviously I still don't understand xmlhttprequest to make it work. The effect I was after with the menu buttons/classname changes is working perfectly (thanks), but the iframe equivalent isn't there. Not sure what I messed up. The desired (for now) content in that dynamic div/frame is at ./test-tour/java-only.html (created also a javatour.html without the full html page structure - just the body - no difference though). If you can see what the problem is, that would be super.

    Also, if you could point me to the best example/explanation of xmlhttprequest, that would be super.

    loklomedia.com/testing/tyler/ajaxtest.html - JS Example iframe/xmlhttprequest

  8. #8
    Experienced Member jameson's Avatar
    Join Date
    Aug 2008
    Posts
    272
    Before you get too far into the world of ajax and xmlhttprequests, I think you should just use jQuery. I recently designed this site using the jQuery 'show' and 'hide' functions to do what you're trying to do, and it was really pretty easy. As an example, here's the code I'm using behind one of my buttons:

    Code:
    $('a#shots_button').click(function(event){  $('#rundown').hide();
      $('#grind').hide();
      $('#branding').hide();
      $('#projects').hide();
      $('#contact').hide();
      $('#shots').fadeIn("slow");
    Write similar code for each button on your page and you'll be all set.

  9. #9
    Junior Member
    Join Date
    May 2008
    Posts
    35
    time limit says I'll be sticking with iframe.
    thanks.

  10. #10
    likes chips. Scriptage's Avatar
    Join Date
    May 2008
    Location
    Yorkshire
    Posts
    2,760
    Hey bud, this is where I went wrong:

    HTML Code:
    <!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>JS</title>
    <style type="text/css">
    .active{
    background-color: red;
    color: white;
    }
    .inactive{
    background-color: green;
    color: yellow;
    }
    #cframe{
    width: 500px;
    height: 500px;
    }
    </style>
    <script type="text/javascript">
    /* <![CDATA[ */
    function GetRequestObject(){
       var XMLHttp;
       /*@cc_on
        @if (@_jscript_version >= 5)
        try {
          XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {
            XMLHttp = false;
          }
        }
       @else
       XMLHttp = false;
       @end @*/
       if (!XMLHttp && typeof XMLHttpRequest != 'undefined') {
        try {
          XMLHttp = new XMLHttpRequest();
        } catch (e) {
          XMLHttp = false;
        }
       }
       return XMLHttp;
    }
     
    function Page(Source, HeaderFile, ID){
    if(!window['GPList']){
    window['GPList'] = new Array();
    }
    this.Source = Source;
    this.HeaderFile = HeaderFile;
    this.Self = document.getElementById(ID);
    this.Active = function(Bool){
       if(Bool){
       this.Self.className = 'active';
       this.Self.firstChild.className = 'active';
       document.getElementById('headimg').src = this.HeaderFile;
       Request = GetRequestObject();
    
       if(!Request){
    
       window.location.href = this.Self.getElementsByTagName('a')[0].href;
       }
    
       Request.open("GET", this.Source ,true);
     
       Request.onreadystatechange = function(){
    
       if(Request.readystate == 4 || Request.readyState == 4){ // Successfully sent
    
       var File = Request.responseText.substring(Request.responseText.indexOf('<body>'), Request.responseText.indexOf('</body>')+7);    
    
       document.getElementById("content").innerHTML = File;
     
       }
       };
     
    Request.send(null);
     
       }else{
       this.Self.className = 'inactive';
       this.Self.firstChild.className = 'inactive';
     
       }
     
    }
    window['GPList'].push(this);
    return this;
    }
    function LoadPage(Obj){
    
    var ID = Obj.parentNode.id;
       for(var i=0; i<GPList.length; i++){
     
          if(GPList[i].Self.id == ID){
          GPList[i].Active(1);
          }else{
          GPList[i].Active(0);
          }
       }
    return false;
    }
    /* ]]> */
    </script>
    </head>
    <body>
    <div id="header">
       <img id="headimg" src="default.jpg" alt="Page Heading" />
    </div>
    <div id="links">
    <ul>
        <li class="inactive" id="lib">
          <a href="libertyplan.html" onclick="LoadPage(this); return false">The Liberty</a>
        </li>
        <li class="inactive" id="brk">
          <a href="brooklynplan.html" onclick="LoadPage(this); return false;">The Brooklyn</a>
        </li>
    </ul>
    </div>
    <div id="content">
    </div>
    <script type="text/javascript">
    new Page('libertyplan.html', 'images/libertyplan.png', 'lib');
    new Page('brooklynplan.html', 'images/brooklynplan.png', 'brk');
    </script>
    </body>
    </html>
    Firefox has the property readyState whereas it is readystate in IE. The code above will work cross browser.

    I'll try to post an AJAX tutorial at some point.

    Regards

    Carl
    Last edited by Scriptage; 02-03-2009 at 10:31 PM.

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Similar Threads

  1. Problem with IE6
    By natidragon in forum CSS Forum
    Replies: 2
    Last Post: 14-02-2009, 05:26 PM
  2. problem with the css , help please
    By crazy.works in forum CSS Forum
    Replies: 0
    Last Post: 13-07-2008, 03:39 AM
  3. Problem with IE
    By spnwebdesign in forum Graphic Design and Web Design Help
    Replies: 7
    Last Post: 24-02-2008, 04:07 PM
  4. IE.. problem
    By maiova in forum CSS Forum
    Replies: 7
    Last Post: 09-12-2007, 07:23 AM
  5. CSS / IE 6 -- what's the problem?
    By inbetweendays in forum CSS Forum
    Replies: 3
    Last Post: 03-07-2007, 03:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


The Graphics Forum Web Design Stuff Free Decent Downloads Free Quality Wallpapers Graphics Forum Free Vista Themes
The Top The Best Images Tech Talk 247 Logo Design - $149 Affordable Stock Vector Illustrations Creativecurio - Design Blog Graphic Design Advertising

Check the forum often for the latest design announcements. Everything from graphic design and web design, to films and music. Estetica is a great place for people to get together and help each other out.



Web Hosting - UK Web Hosting services for business or personal website hosting needs.

Dedicated Servers - A full range of Managed Dedicated Server solutions suitable for all your requirements.

Graphic Design Blog | Web Design Forum | Graphic Design and Print Forum | Graphic Design Links | Advertise On This Site

Web Design UK | Vision.To Design | Leaflet Printing | Estetica Design Forum's Privacy Policy

Flyer Printing | Photography Blog | Design Forum Links | Logo Design | Graphic Design Social Network | Logo Design

Graphic Design Tutorials | Logo Designer | UK Logo Design Studio | Land for sale | Vector Art Blog | Leaflet Printing

Free Web Hosting | Custom Logo Design - $149 Only | Affordable Print Design Templates | Small Business Logo Design | Company Logo Design

Logo Design Service | Logo Design Firm | Logo Design Reseller | Custom Logo Design | Letterhead Printing | Flyer Printing | Business Card Printing

Printing | Leaflet Printing | Online Backup | T-Shirt Printing | Personalised Mugs | Canvas Printing | Free Web Hosting Comparison Site