Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Common Javascript issue? [Solved]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Off the Wall
View previous topic :: View next topic  
Author Message
doubleagent
Guru
Guru


Joined: 15 Apr 2005
Posts: 444
Location: 127.0.0.1

PostPosted: Tue Apr 06, 2010 5:57 pm    Post subject: Common Javascript issue? [Solved] Reply with quote

I have this code inside the html head:
Code:
    <script type="text/javascript" src="scripts/jquery.js"></script>
    <script type="text/javascript">
      String.prototype.endsWith = function(pattern) {
        var d = this.length - pattern.length;
        return d >= 0 && this.lastIndexOf(pattern) === d;
      };
      function showServices() {
        $("#applications_content").hide();
        $("#portfolio_content").hide();
        $("#about_content").hide();
        $("#contact_content").hide();
        $("#blog_content").hide();
        $("#services_content").show('fast');
      }
      $(document).ready(function() {
        var url = window.location.href;
        if(url.endsWith("services")) {
          //alert('yay');
          showServices();
        }
        $("#services").click(function() {
          showServices();
        });
      });
    </script>


If I uncomment the line "alert('yay')", then showServices() executes as expected, hiding the div's and displaying #services_content on page load. If the line is commented out then showServices executes but it's effects aren't seen (no div's are hidden). I'm currently only testing on FF 3.5.8. What's the deal?
_________________
shickapooka wrote:
i think they programmed [otw] based on a right-wing jewish-nigger-nazi, his gay, retarded, left-wing love slave with webbed feet, and their three headed cat that poops uncontrollably. the cat is also an apple fanboy


Last edited by doubleagent on Tue Apr 06, 2010 8:21 pm; edited 1 time in total
Back to top
View user's profile Send private message
jho
Apprentice
Apprentice


Joined: 24 May 2007
Posts: 153
Location: Jyväskylä, Finland

PostPosted: Tue Apr 06, 2010 6:08 pm    Post subject: Reply with quote

The elements haven't yet been rendered on the document?

Try moving the $(document).ready() function to after the divs.
_________________
"I'm sorry, I only accept criticism in the form of sed expressions."
Back to top
View user's profile Send private message
Butts McCokey
Advocate
Advocate


Joined: 23 Apr 2004
Posts: 3313

PostPosted: Tue Apr 06, 2010 6:25 pm    Post subject: Reply with quote

what jho said
_________________
Since the bible and the church are obviously mistaken about where we came from, how can we trust them with where we're going?

"An eye for an eye will make us all blind" - Gandhi
Back to top
View user's profile Send private message
doubleagent
Guru
Guru


Joined: 15 Apr 2005
Posts: 444
Location: 127.0.0.1

PostPosted: Tue Apr 06, 2010 7:46 pm    Post subject: Reply with quote

That doesn't fix it. Behavior is the same.
_________________
shickapooka wrote:
i think they programmed [otw] based on a right-wing jewish-nigger-nazi, his gay, retarded, left-wing love slave with webbed feet, and their three headed cat that poops uncontrollably. the cat is also an apple fanboy
Back to top
View user's profile Send private message
doubleagent
Guru
Guru


Joined: 15 Apr 2005
Posts: 444
Location: 127.0.0.1

PostPosted: Tue Apr 06, 2010 8:21 pm    Post subject: Reply with quote

I filled in all the gaps and it seems to work now:

Code:
    <script type="text/javascript" src="scripts/jquery.js"></script>
    <script type="text/javascript">
      String.prototype.endsWith = function(pattern) {
        var d = this.length - pattern.length;
        return d >= 0 && this.lastIndexOf(pattern) === d;
      };

      function hideAll() {
        $("#services_content").hide();
        $("#applications_content").hide();
        $("#portfolio_content").hide();
        $("#about_content").hide();
        $("#contact_content").hide();
        $("#blog_content").hide();
      }

      $(document).ready(function() {
        var url = window.location.href;

        hideAll();
       
        if(url.endsWith("applications")) {
          $("#applications_content").show('fast');
        }
        else if(url.endsWith("portfolio")) {
          $("#portfolio_content").show('fast');
        }
        else if(url.endsWith("about")) {
          $("#about_content").show('fast');
        }
        else if(url.endsWith("contact")) {
          $("#contact_content").show('fast');
        }
        else if(url.endsWith("blog")) {
          $("#blog_content").show('fast');
        }
        else {
          $("#services_content").show('fast');
        }

        $("#services").click(function() {
          hideAll();
          $("#services_content").show('fast');
        });
        $("#applications").click(function() {
          hideAll();
          $("#applications_content").show('fast');
        });
        $("#portfolio").click(function() {
          hideAll();
          $("#portfolio_content").show('fast');
        });
        $("#about").click(function() {
          hideAll();
          $("#about_content").show('fast');
        });
        $("#contact").click(function() {
          hideAll();
          $("#contact_content").show('fast');
        });
        $("#blog").click(function() {
          hideAll();
          $("#blog_content").show('fast');
        });
      });

    </script>

Horrible code, I know...but I don't really plan on adding any more pages so whatever.

My only concern is that, in FF, if the page is already loaded and I just alter the URL in the address bar (change 'about' to 'services') and press enter, the page will stay the same. If I open a new tab and copy+paste the new url or press the reload button then the page loads as expected. This is likely just an FF issue, though.

I'll mark it solved.

EDIT: slightly improved the code.
_________________
shickapooka wrote:
i think they programmed [otw] based on a right-wing jewish-nigger-nazi, his gay, retarded, left-wing love slave with webbed feet, and their three headed cat that poops uncontrollably. the cat is also an apple fanboy
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Off the Wall All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum