Where do I put javascript in ContentPlaceHolder?

I'm using a ContentPlaceHolder and would like to move the onload="Carousel()" from the body tag of of the .master page

body onload="Carousel()"

but I don't know where to put it in the content page.

I'm trying to implement this

http://www.dynamicdrive.com/dynamicindex14/carousel.htm

Upvotes: 0

Views: 4404

Answers (2)

Tyler
Tyler

Reputation: 514

The reason why it's currently on

<body onload="... 

is because the script needs to run after the document has been loaded in to the browser. At that time the DOM has finished loading and you have access to all of your elements.

You have two options:

Place the call to Carosel() as far towards the bottom of the page as you can go. By doing this the script won't be run until the rest of the page has loaded (loads from top to bottom).

or

handle the body onload event

<script type="text/javascript">
    body.onload = Carosel;
</script>

You could also use jQuery:

<script type="text/javascript">
    $(document).ready(Carosel);
</script>

Upvotes: 2

Rich
Rich

Reputation: 671

It may be overkill, but if you have access to the jQuery javascript library on the page, the following javascript can be put on your content page:

$(document).ready(function() {  
  Carousel();
});

Upvotes: 0

Related Questions