Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MegaMan1311

macrumors regular
Original poster
Jun 30, 2007
216
0
USA
I need a script to use if this is possible... I'm pretty sure it is, but I don't know because I am not a very advanced at coding.

I need to display some content if Javascript is enabled, but if it is not, show different content. I can run php on the site if its needed.

I am making an ajax navigation bar, but need to make an option for those who don't have javascript. I thought of using the noscript tag, but if they didn't have javascript, they would see both the ajax and the simple version because the ajax one involves images.

I hope you understand what I mean. Thanks in advance.

MegaMan1311
 

tominated

macrumors 68000
Jul 7, 2006
1,723
0
Queensland, Australia
I need a script to use if this is possible... I'm pretty sure it is, but I don't know because I am not a very advanced at coding.

I need to display some content if Javascript is enabled, but if it is not, show different content. I can run php on the site if its needed.

I am making an ajax navigation bar, but need to make an option for those who don't have javascript. I thought of using the noscript tag, but if they didn't have javascript, they would see both the ajax and the simple version because the ajax one involves images.

I hope you understand what I mean. Thanks in advance.

MegaMan1311

The noscript method should work seeing as AJAX is just javascript with a little xml sprinkled on top.
 

MegaMan1311

macrumors regular
Original poster
Jun 30, 2007
216
0
USA
The noscript method should work seeing as AJAX is just javascript with a little xml sprinkled on top.
Yes, but my navigation is made of images with ajax menus coming from it. I want the images to be linked if there if javascript isn't enabled. If it is enabled, I don't want it to be linked, to make it simpler to the user.
 

tominated

macrumors 68000
Jul 7, 2006
1,723
0
Queensland, Australia
Yes, but my navigation is made of images with ajax menus coming from it. I want the images to be linked if there if javascript isn't enabled. If it is enabled, I don't want it to be linked, to make it simpler to the user.

oh. in that case can you post the code so we know exacly what we are dealing with?
 

iSee

macrumors 68040
Oct 25, 2004
3,539
272
Yes, but my navigation is made of images with ajax menus coming from it. I want the images to be linked if there if javascript isn't enabled. If it is enabled, I don't want it to be linked, to make it simpler to the user.

You can set up a link like this:
<a href="someplace.html" onclick="javascript: someFunction(); return false;"><img ... /></a>

When this runs in a browser without javascript onclick is ignored. When the user clicks the link, they navigate to someplace.html.

Notice that onclick returns false. That tells the browser to NOT follow the link. It does, however, call someFunction(). If you don't want clicking the link to do anything under javascript, just make it onclick="javascript: return false;"

Does this help what you are doing? If not, post some code. Worse comes to worse, setup the page with the links the way you want them when javascript isn't available. Then use javascript when the page loads to modify the DOM to remove/change the links.
 

MegaMan1311

macrumors regular
Original poster
Jun 30, 2007
216
0
USA
Ajax-powered menus? By the sound of it, that seems pretty inefficient. Why not load all of the navigation with the page?
I have a horizontal navigation bar, so I need menus that drop down from each one.. I did it like this to save space. The ajax is just to make it nicer looking. If I didn't use ajax, I would have to use javascript. Why use javascript when you can use ajax?

oh. in that case can you post the code so we know exacly what we are dealing with?

Unfortunately, I am still working with the site, so the ajax isn't implemented yet. I can show you what I am doing, but it won't have the menus working.

You can set up a link like this:
<a href="someplace.html" onclick="javascript: someFunction(); return false;"><img ... /></a>

When this runs in a browser without javascript onclick is ignored. When the user clicks the link, they navigate to someplace.html.

Notice that onclick returns false. That tells the browser to NOT follow the link. It does, however, call someFunction(). If you don't want clicking the link to do anything under javascript, just make it onclick="javascript: return false;"

Does this help what you are doing? If not, post some code. Worse comes to worse, setup the page with the links the way you want them when javascript isn't available. Then use javascript when the page loads to modify the DOM to remove/change the links.
Yes. It works great! One question, I doubt it is possible, but is there a way make it so that there isn't even a link there if javascript is enabled?

Thanks.
 

ppc_michael

Guest
Apr 26, 2005
1,498
2
Los Angeles, CA
I run a video game news and guides site. Thus it has many pages. Doing it like I have it set up saves space. The ajax is just to make it nicer looking. If I didn't use ajax, I would have to use javascript. Why use javascript when you can use ajax?

Ajax is Javascript. It's only meant for sending conditional data to/from the server. If you're doing things just to "save space" then all you really need is to show/hide DIVs.

One question, I doubt it is possible, but is there a way make it so that there isn't even a link there if javascript is enabled?

You could place the link in a DIV that is set to display:hidden (CSS) by default, and then use Javascript to reveal it. So if Javascript isn't running, it is never revealed.
 

MegaMan1311

macrumors regular
Original poster
Jun 30, 2007
216
0
USA
Ajax is Javascript. It's only meant for sending conditional data to/from the server. If you're doing things just to "save space" then all you really need is to show/hide DIVs.



You could place the link in a DIV that is set to display:hidden (CSS) by default, and then use Javascript to reveal it. So if Javascript isn't running, it is never revealed.
I wanted ajax for a bit of a dynamic effect.

Would it be possible to hide a div with javascript? If so, how would I go about doing it?
 

CoreWeb

macrumors 6502
Mar 2, 2007
456
0
Edge of reason
I wanted ajax for a bit of a dynamic effect.

Would it be possible to hide a div with javascript? If so, how would I go about doing it?

Ajax does absolutely NO dynamic effects.

It sounds to me like you don't know what Ajax is. Ajax is Asynchronous Javascript and XML. It is meant to allow you to download content from the server. It is all done THROUGH Javascript.

Hiding a div in Javascript is as simple as
Code:
document.getElementById("divid").style.visibility = "hidden";

That's javascript. There is no such thing as hiding a div in Ajax. Javascript is a language, Ajax is a certain convention used in some Javascript scripts. You may benefit from reading some Javascript tutorials online. There are some good ones (if you want I can try to find some for you), and you will learn the limitations/capabilities of Javascript.

Ajax should definitely not be used for menus, though. Ajax downloads new content from the server; if you were using that in your menus you'd have to make the client's computer connect to the server any time they wanted to view the menu! That is quite inefficient. Instead, the div should be hidden by default (the "style" attribute for it should specify "visibility:hidden") and then you make it appear with Javascript.

Also, as a side note, it may also advisable to store your menus not in divs but in more meaningful "lists" which are then styled through CSS:
Code:
<ul>
    <li>menu 1
        <ul>
            <li>sub item 1</li>
            <li>sub item 2</li>
         </ul>
     </li>
     <li>menu 2
         <ul>
             <li>sub item 1</li>
         </ul>
     </li>
</ul>

Then you'd use CSS to make them all appear on one line, and strip the bullets, etc. The benefit is that it is more meaningful, and if anyone ever strips both the stylesheet and the Javascript away, it would still be readable as a list.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.