This is Interesting: Free Magazines for Graphics designers and webmasters
Home > Archive > Dreamweaver > February 2006 > How to add JavaScript to template-based pages?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to add JavaScript to template-based pages?
|
|
| Tintin81 2006-02-07, 10:39 pm |
| Hello,
Please have a look at http://www.pixellover.net
All my pages are based on one and the same Dreamweaver template. However, from
time to time I would like to include some JavaScript on individual pages. Is
there any way to make this work without using the Head section of the HTML? The
Head section can't be edited in any of my template-based pages. How can I add
some "OnLoad" behaviours to my Body tags?
Can anybody help?
| |
| Joe Makowiec 2006-02-07, 10:39 pm |
| On 27 Jan 2006 in macromedia.dreamweaver, Tintin81 wrote:
> Please have a look at http://www.pixellover.net
> All my pages are based on one and the same Dreamweaver template.
> However, from
> time to time I would like to include some JavaScript on individual
> pages. Is there any way to make this work without using the Head
> section of the HTML? The Head section can't be edited in any of my
> template-based pages.
UTSL.[1] From your page:
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
> How can I add some "OnLoad" behaviours to my Body tags?
This one is more problematic. It's been a while since I've done it,
but if I recall, it works something like this:
- In your template, add onload="myfunction();" to the body tag:
<body onload="myfunction();">
- In your head region, add a script tag:
<!-- InstanceBeginEditable name="head" -->
<!-- Add functions here -->
<script language="JavaScript" type="text/JavaScript">
function whatever() {
// Do something
}
</script>
<script language="JavaScript" type="text/JavaScript">
function myfunction() {
// Add calls to functions you want to call here
whatever();
}
</script>
<!-- InstanceEndEditable -->
Note - I haven't done this in a while, and my recall may be off.
You'll probably need to experiment to get it right.
[1] http://en.wikipedia.org/wiki/UTSL
--
Joe Makowiec
http://makowiec.net/
Email: http://makowiec.net/email.php
| |
| Mick White 2006-02-07, 10:39 pm |
| Tintin81 wrote:
> Hello,
>
> Please have a look at http://www.pixellover.net
> All my pages are based on one and the same Dreamweaver template. However, from
> time to time I would like to include some JavaScript on individual pages. Is
> there any way to make this work without using the Head section of the HTML? The
> Head section can't be edited in any of my template-based pages. How can I add
> some "OnLoad" behaviours to my Body tags?
>
******Editable Region***************
* <script type="text/javascript"> *
* onload=function(){ *
* myFunction(); *
* anotherFunction() *
* alert(New Date()); *
* } *
* </script> *
****Editable Region*****************
This can go anywhere in the page.
Mick
| |
| Tintin81 2006-02-07, 10:39 pm |
| Thanks for your help so far.
Mick, I tried to implement your idea, but it doesn't seem to work, probably
because I'm still a JavaScript learner. What I'm trying to achieve is a random
banner rotation effect. Have a look at the following code:
<!-- InstanceBeginEditable name="EditContent" -->
<script language="JavaScript" type="text/JavaScript">
<!--
onload=function myPix = new Array("images/banner1.jpg", "images/banner2.jpg",
"images/banner3.jpg")
imgCt = myPix.length
onload=function choosePic() {
if (document.images) {
randomNum = Math.floor ((Math.random() * img(t))
document.myPicture.src = myPix
}
}
//-->
</script>
<div id="banner"><img src="images/banner1.jpg" name="myPicture" alt="Banner" />
</div>
If only I could add an "onload" attribute to my BODY tag. But that doesn't
work since i assigned a unique page ID to it. It's all very confusing... :-(
| |
| Mick White 2006-02-07, 10:39 pm |
| Tintin81 wrote:
> Thanks for your help so far.
> Mick, I tried to implement your idea, but it doesn't seem to work, probably
> because I'm still a JavaScript learner. What I'm trying to achieve is a random
> banner rotation effect. Have a look at the following code:
>
>
>
>
> <!-- InstanceBeginEditable name="EditContent" -->
<script type="text/javascript">
myPix=
["images/banner1.jpg",
"images/banner2.jpg",
"images/banner3.jpg"];
onload=function(){
if(document.images){
document.images["myPicture"].src=
myPix[new Date().getTime()%myPix.length];
}
}
</script>
This won't work if you have an onload handler in your body tag.
Mick
>
> <script language="JavaScript" type="text/JavaScript">
> <!--
> onload=function myPix = new Array("images/banner1.jpg", "images/banner2.jpg",
> "images/banner3.jpg")
> imgCt = myPix.length
>
> onload=function choosePic() {
> if (document.images) {
> randomNum = Math.floor ((Math.random() * img(t))
> document.myPicture.src = myPix
> }
> }
> //-->
> </script>
>
> <div id="banner"><img src="images/banner1.jpg" name="myPicture" alt="Banner" />
> </div>
>
>
>
> If only I could add an "onload" attribute to my BODY tag. But that doesn't
> work since i assigned a unique page ID to it. It's all very confusing... :-(
>
| |
| Tintin81 2006-02-07, 10:39 pm |
| Hey Mick!
A thousand thanks!!! Your code works great! You can see it in action on the
index page:
http://www.pixellover.net/index.htm
I also added another function to preload the banner images on all the other
pages, but I'm not sure if it really makes that much of a difference since my
internet connection here is pretty fast anyway.
| |
| Murray *TMM* 2006-02-07, 10:40 pm |
| > some "OnLoad" behaviours to my Body tags?
Read DW's F1 Help about editable attributes and how to use them.
--
Murray --- ICQ 71997575
Team Macromedia Volunteer for Dreamweaver
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com - Template Triage!
http://www.projectseven.com/go - DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs, Tutorials & Resources
http://www.macromedia.com/support/search/ - Macromedia (MM) Technotes
==================
"Tintin81" <webforumsuser@macromedia.com> wrote in message
news:drebcs$gda$1@forums.macromedia.com...
> Hello,
>
> Please have a look at http://www.pixellover.net
> All my pages are based on one and the same Dreamweaver template. However,
> from
> time to time I would like to include some JavaScript on individual pages.
> Is
> there any way to make this work without using the Head section of the
> HTML? The
> Head section can't be edited in any of my template-based pages. How can I
> add
> some "OnLoad" behaviours to my Body tags?
>
> Can anybody help?
>
| |
| Mick White 2006-02-07, 10:40 pm |
| Tintin81 wrote:
> Hey Mick!
>
> A thousand thanks!!! Your code works great! You can see it in action on the
> index page:
> http://www.pixellover.net/index.htm
>
> I also added another function to preload the banner images on all the other
> pages, but I'm not sure if it really makes that much of a difference since my
> internet connection here is pretty fast anyway.
>
>
>
//Not quite right, sb as follows:
function MM_preloadImages() { //v3.0
var d=document;
if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
for(i=0;i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image;
d.MM_p[j++].src=a[i];
}
}
}
myPix=["images/banner_home_1.jpg", "images/banner_home_2.jpg",
"images/banner_home_3.jpg"];
onload=function(){
MM_preloadImages();
if(document.images){
document.images["BannerRotation"].src=
myPix[new Date().getTime()%myPix.length];
}
}
Mick
|
|
|
| | Copyright 2003 - 2008 forum4designers.com Software forum Computer Hardware reviews |
|