Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run jquery script if html page is the home page...?

I'm working with an external team with our website and they recently added one of my scripts to the .NET MasterPage of the site... well it did finally get my script running but now... it loads Banners on 'every' page on the site.

How can I write an 'if' statement that basically says... if this is the home page... run this script... if not don't...?

like image 690
chrisb Avatar asked Sep 14 '10 23:09

chrisb


People also ask

Where should jQuery be placed HTML?

It's always a good practice to add jQuery code in footer i.e. just before the closing </body> tag. If you have not done that, then use the defer attribute. The defer attribute is used to specify that the script execution occurs when the page loads.

How do I run a jQuery script?

Declare the jQuery file path in the <script> tag inside the <head> tag section. Example 1: Create an HTML file & add below code into your HTML file.

What are two way to include jQuery in your web pages?

There are two ways to include jQuery in a project, which is to download a local copy or link to a file via Content Delivery Network (CDN).


1 Answers

I'm posting another answer in case you can't implement the Master Page solution.

You could use a flag element to tell jQuery it's the homepage, because the URL solutions posted earlier can easily break.

Somewhere in your Homepage content, simply place this.

<span id="homepage-flag" style="display: none" />

And then using jQuery, check if the element exists and run your code. It's a pretty poor solution but it will work if you can't get my other answer to work.

if($("#homepage-flag").length > 0) {
    // run code for homepage
}
like image 66
Marko Avatar answered Oct 25 '22 13:10

Marko