1

Basically, I have several pages on my website where I am using PHP to include the content in the page, and using jQuery to "switch" between the content by setting the display in CSS to "none" or "block". However, I am struggling to get it to work in the main page with the menu. So I need when the user clicks on the appropriate link, the page will change to the page where the content is contained, but also apply the appropriate CSS as to display the appropriate content. So far I have (URL blocked out):

<script type="text/javascript">
    $(document).ready(function(){
        $('#ourMissionMenu').click(function() {
                window.location = 'http://beta.***.com/aboutUs.php';
                $('#rightReplace').css({
                  "display":"none"
                  });
                  $('#ourHistory').css({
                  "display":"none"
                  });
                  $('#meetTeam').css({
                  "display":"none"
                  });
                  $('#communityOutreach').css({
                  "display":"none"
                  });
                  $('#connectUs').css({
                  "display":"none"
                  });
                  $('#blog').css({
                  "display":"none"
                  });
                   $('#ourMission').css({
                  "display":"block"
                  }); 
        });
});


</script>
4
  • What isn't working about the above code? What happens now? Commented Jan 23, 2012 at 17:54
  • You can just use show or hide instead of changing the display property. Commented Jan 23, 2012 at 17:55
  • 1
    Your script will stop when the page is changed to "aboutUs.php". You can control the data that will display in the server side... Commented Jan 23, 2012 at 17:56
  • The "aboutUs" page loads... but not the CSS. Thanks for the tip on display. Commented Jan 23, 2012 at 18:00

4 Answers 4

1

If you change the location of the window, all javascript from the current page will cease. You cannot expect javascript from one page to control content on another page. This would be a massive security violation.

You need the javascript to run on "aboutUs.php", or dynamically load the aboutUs content into your current page. Or, better still, have aboutUs.php have the correct css in the first place.

Sign up to request clarification or add additional context in comments.

5 Comments

The CSS on aboutUs is correct. Basically the the links on the menu on index.php link to different content within aboutus, which I originally set to change using javascript and css.
This is where you should leverage php and not javascript. You should have aboutUs.php accept a parameter which triggers changes to the css. I.e. you would call window.location = 'http://beta.***.com/aboutUs.php?source=index';. Your php would then change the css based on the value of source, for instance.
In other words, php is all about dynamic content, so why act like your content will be static, instead of letting it be dynamic?
I believe you would be right. However, how can I do this in PHP?
Best asked in a new question... but looks like you have already: stackoverflow.com/questions/8977065
0

Can't aboutUs.php just link to a CSS file that has the correct styles? Why do they need to be set in Javascript?

It also looks like the css() calls will be applied to the current page, and not the target page.

2 Comments

This menus is on the "index.php" and the content I need to display is on the "aboutUs.php" page. So I need to load the aboutUs page, THEN change the css to "show" the proper content.
How can I apply the CSS to the page I need?
0

Once you change the window.location property, the browser will redirect the user to the specified URL and the execution of JavaScript will stop immediately.

So your .css() function calls are not being run but that doesn't matter much because they would be altering the DOM on the current page, not the next page.

Those .css() function calls need to be run on the next page, something like this:

First Page --

$(function(){
    $('#ourMissionMenu').click(function() {
        window.location = 'http://beta.***.com/aboutUs.php';
    });
});

Second Page (aboutUs.php) --

$(function(){
    $('#rightReplace').css({
        display:"none"
    });
    $('#ourHistory').css({
        display:"none"
    });
    $('#meetTeam').css({
        display:"none"
    });
    $('#communityOutreach').css({
        display:"none"
    });
    $('#connectUs').css({
         display:"none"
    });
    $('#blog').css({
        display:"none"
    });
    $('#ourMission').css({
        display:"block"
    });
});

But this would be pretty strange if you are dynamically building the aboutUs.php page. You might as well leverage your use of PHP and set the display of the element with PHP instead of JavaScript (which is client-side, meaning it requires the user's browser to do work).

Comments

0

On top of what Paul and Sérgio mentioned, you can concoct the selectors like below rather than multiple blocks.

$('#rightReplace,#ourHistory,#meetTeam,#communityOutreach,#connectUs,#blog,#ourMission').hide();

3 Comments

Or: $('#rightReplace,#ourHistory,#meetTeam,#communityOutreach,#connectUs,#blog,#ourMission').hide()
Thanks for the cleanup. Im new to web dev.
You got it better ... bingo. I have now updated the masterpiece :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.