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

4

11

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...?

Fairspoken answered 14/9, 2010 at 23:23 Comment(4)
Isn't the point of a master page to include the data in every page? This sounds horribly like "Doctor! Doctor! It hurts when I fire the gun at my foot!"Jiles
I'm not sure how helpful that was, but it was funny...!Fairspoken
The implication is "Put the <script> in the specific page instead of the master page". That doesn't answer the question, as written, though.Jiles
yeah, we ended up placing it in the MasterPage after we couldn't get the script to run elsewhere.Fairspoken
H
38

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
}
Hydrostatics answered 14/9, 2010 at 23:41 Comment(6)
@Fairspoken I feel bad now for providing this (ugly) alternative - why can't you do use the code above? It's a much better solution.Hydrostatics
lol. I can't say I've ever posted 2 answers, one of which got 5 upvotes and the other got accepted.Hydrostatics
I'm just the frontend java guy... and don't have access to the Master Pages... I just need a 'dirt' fix until those guys get back in tomorrow... ;-)Fairspoken
This answer saved my life.Cammie
One tip, use "js-homepage-flag", then you know that this element will be used in your javascript and in your javascript only.Rader
gross... I cant wait to use this! +1 :)Trapp
H
6

How about a script Content Place holder that's inside the <head> of the MasterPage, and then placing content inside the placeholder from your homepage.

Basically..

In your Master Page

<head>
<title>hello</title> etc...
// add jQuery here

<asp:ContentPlaceHolder ID="jQueryCode" runat="server"></asp:ContentPlaceHolder>

And then in your Home Page

<asp:Content ContentPlaceHolderId="jQueryCode" runat="server">
    // run jQuery script here
</asp:Content>

Also - if you're not using jQuery on the other pages, you can remove it from the MasterPage and add it right above your script inside the home page <asp:Content />

Hydrostatics answered 14/9, 2010 at 23:27 Comment(2)
+1 Most any other way of doing this would smell of the equivalent of requiring a parent class to know about its sub classes.Gipps
I am in dialog with the backend devs on implementing this solution.Fairspoken
F
0

You shouldn't, but you could probably do something like this:

if(window.location.pathname == "{home page}")
{
  //run home page jquery.
}

BUT... my advise would be to create a content section on the home page that gets placed into the HEAD and put the jQuery into there instead of the masterpage. No reason to include it in the masterpage if it's not used everywhere...

Foreignism answered 14/9, 2010 at 23:26 Comment(0)
R
0

if you're using vb.net, you can use the content placeholder as mentioned above but just use a little codebehind on the masterpage vb file to hide/show the placeholder:

 If Request.Url.AbsolutePath.ToLower = "/index.aspx" Then
    jQueryCode.Visible = true
End If

(make sure to set the default view to visible:false first before rebuilding like this:)

<asp:Content ContentPlaceHolderId="jQueryCode" runat="server" visible="false"> 
    // run jQuery script here 
</asp:Content> 
Roebuck answered 17/1, 2012 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.