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...?
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.
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.
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).
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With