473,770 Members | 7,213 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a .js script for sliding a text banner

I've been using a javascript in an html file for a banner slider, and
it works as desired. But I'd like to use it on more than one page and
it would be great if I could transfer the code to a .js file and call
it with the
<script src="filename.j s"></script> tags as I do for many other .js
files. But, when I try to do that way, it doesn't work right. It will
display the banner text, but only on the 0,0 page coordinate and
doesn't slide.

The script employs a function called positionIt() which determines the
window size, left and top points. Is the problem that it's running in
a called script and therefore doesn't have reference to the html page,
or is it likely to be some other reason? Does it need values passed to
it by the <Script> statement? Clues or guidance?

- Jakej

Jul 23 '05 #1
8 4687
Jakej wrote:
I've been using a javascript in an html file for a banner slider, and
it works as desired. But I'd like to use it on more than one page and
it would be great if I could transfer the code to a .js file and call
it with the
<script src="filename.j s"></script> tags as I do for many other .js
files. But, when I try to do that way, it doesn't work right. It will
display the banner text, but only on the 0,0 page coordinate and
doesn't slide.

The script employs a function called positionIt() which determines the
window size, left and top points. Is the problem that it's running in
a called script and therefore doesn't have reference to the html page,
or is it likely to be some other reason? Does it need values passed to
it by the <Script> statement? Clues or guidance?


Without seeing the code or HTML it relates too, it's a tad difficult to
say. If you want speculation, then likely the structure of your HTML
is a little different on the extra pages and your script is not getting
the co-ords right.

Here is some stuff from Quirksmode on position of elements in the
page:

<URL:http://www.quirksmode. org/js/findpos.html>

--
Rob
Jul 23 '05 #2

Roughly, I'd assume you're using other <script> blocks or included .js
files in it, and they're using same named global vars OR same object over
the same runtime from different function calls, but no, there's no
difference if you put it with <script> or just include the .js file, there
isn't any. The issue is you're just copy/pasting.

Danny
On Sun, 03 Jul 2005 16:53:12 -0700, Jakej <Ja***@variagat e.com> wrote:


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #3
Rob: Thanks for your reply and reference to quirksmode. Here's the code
I'm using. Perhaps you can see where the problem lies. I have tried
this within a "normal" page and a test page that has no other html
code-- with the same result, namely that the banner text appears at 0,0.
This, of course, is when I call the code with <script
"src=filename.j s"></script>. When the javascript is in the html doc, it
works fine.

document.write( " <!-- new script: static slider -->")
document.write( "<div id='staticbanne r' style='position :absolute;'>")
document.write( "<a href='linkpage. htm?adslider'
target=_blank>B annerText</a>")
document.write( " </div>")
document.write( " <script>")
document.write( " //define universal reference to
'staticbanner'" )
document.write( " var crossobj=docume nt.all?
document.all.st aticbanner :")
document.write( " document.getEle mentById?")
document.write( "
document.getEle mentById('stati cbanner') :")
document.write( " document.static banner")
document.write( " function positionit(){")
document.write( " //define universal dsoc left point")
document.write( " pageXOffset")
document.write( " //define universal dsoc top point")
document.write( " var dsoctop=documen t.all?
document.body.s crollTop :")
document.write( " pageYOffset")
document.write( " //define universal browser window
width")
document.write( " var window_width=do cument.all?
document.body.c lientWidth")
document.write( " : window.innerWid th")
document.write( " //if the user is using IE 4+ or NS6+")
document.write( " if
(document.all|| document.getEle mentById){")
document.write( "
crossobj.style. left=parseInt(d socleft)+")
document.write( " parseInt(window _width)-70")
document.write( " crossobj.style. top=dsoctop+120 ")
document.write( " }")
document.write( " //else if the user is using NS 4")
document.write( " else if (document.layer s){")
document.write( " crossobj.left=" )
document.write( " dsocleft+window _width-60 ")
document.write( " crossobj.top=ds octop+130 ")
document.write( " }")
document.write( " }")
document.write( " setInterval('po sitionit()',200 )")
document.write( " </script>")

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #4
Jake j wrote:
Rob: Thanks for your reply and reference to quirksmode. Here's the code
I'm using. Perhaps you can see where the problem lies. I have tried
this within a "normal" page and a test page that has no other html
code-- with the same result, namely that the banner text appears at 0,0.
This, of course, is when I call the code with <script
"src=filename.j s"></script>. When the javascript is in the html doc, it
works fine.
When you put this in the page, is the following script inside another
element that is relatively positioned?

The generated 'staticbanner' div is absolutely positioned, so unless it
is constrained inside a relatively positioned element it will go to
0,0.

A bigger issue is: why use a script to write a script? Also, why
restrict it to only browsers that know document.all?

All of the script could be written with a single document.write call:

document.write( " <!-- new script: static slider -->")
document.write( "<div id='staticbanne r' style='position :absolute;'>")
document.write( "<a href='linkpage. htm?adslider'
document.write(
" <!-- new script: static slider -->",
"<div id='staticbanne r' style='position :absolute;'>",
"<a href='linkpage. htm?adslider' target=_blank>B annerText</a>",
...
);
target=_blank>B annerText</a>")
document.write( " </div>")
document.write( " <script>")
This script element should have a type attribute
document.write( " //define universal reference to
'staticbanner'" )
When posting code, use 2 or 4 spaces for indents and manually wrap code
so that it can be cut and pasted, then run without errors.
document.write( " var crossobj=docume nt.all?
document.all.st aticbanner :")
document.write( " document.getEle mentById?")
document.write( "
document.getEle mentById('stati cbanner') :")
document.write( " document.static banner")
document.write( " function positionit(){")
document.write( " //define universal dsoc left point")
document.write( " pageXOffset")
Something is missing here...

[...] document.write( " </script>")


When using </ inside document.write, the slash should be quoted:

document.write( " <\/script>")

Put 'staticbanner' inside another div that is relatively positioned and
you're fixed (I think...).

Oh, and your position units should have 'px' appended (I've deleted
that line of code somehwere...)

<div style="position : relative; border: 1px solid red; height: 2em;">
<div id="staticbanne r" style="position : absolute;">
<a href="linkpage. html?adslider" target="_blank" >BannerText</a>
</div>

<script type="text/javascript">
//define universal reference to 'staticbanner'
var crossobj;
if ( document.getEle mentById ) {
crossobj = document.getEle mentById('stati cbanner');
} else if (document.all) {
crossobj = document.all['staticbanner'];
} else if ( document.layers ) {
crossobj = document.static banner;
}

function positionit(){
//define universal dsoc left point
var dsocleft = document.all? document.body.s crollLeft : pageXOffset;

//define universal dsoc top point
var dsoctop = document.all? document.body.s crollTop : pageYOffset;

//define universal browser window width
var window_width = document.all?
document.body.c lientWidth : window.innerWid th;

// if the user is using IE 4+ or NS6+
// Append a unit (say 'px') to top & left
if ( document.all || document.getEle mentById ){
crossobj.style. left = parseInt(dsocle ft)
+ parseInt(window _width) - 70 +'px';
crossobj.style. top = dsoctop + 120 + 'px';
}
//else if the user is using NS 4
else if (document.layer s){
crossobj.left = dsocleft + window_width - 60;
crossobj.top = dsoctop + 130;
}
}
setInterval('po sitionit()',200 );
</script>
</div>

--
Rob
Jul 23 '05 #5
Rob: I tried your revision but it doesn't work any better than the
original. The manner in which you put 'staticbanner' inside another div
that is relatively positioned doesn't seem to be the fix. Just to be
clear: the banner text does appear, but at 0,0 on the page, and it
doesn't slide for relative positioning. If you can think of anything
else to try...?

BTW, I'm beginning to wonder if this has to do with the conventions of
calling javascript code in a .js file from an html file. I use many
such calls successfully, but with .js files that have just html text and
tags. The only ones that don't work are the ones with code. Is there
something that needs to be declared in the html file in addition to
<script src=file.js></script>? Is it necessary to remove the
<script></script> tags within the .js file because of the duplication?
I've tried a few variations but haven't hit on anything that makes this
or simpler javascripts work.

Can you (or anyone else reading this) try it with a simple piece of code
and give me the relevant lines in the calling html file and the .js file
that is called?

~ J

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #6
Jake j wrote:
Rob: I tried your revision but it doesn't work any better than the
original. The manner in which you put 'staticbanner' inside another div
that is relatively positioned doesn't seem to be the fix. Just to be
clear: the banner text does appear, but at 0,0 on the page, and it
doesn't slide for relative positioning. If you can think of anything
else to try...?
The static banner sits where it's told to, inside the relatively
positioned div. I have no idea where the sliding banner goes, you
didn't post the code for it.

BTW, I'm beginning to wonder if this has to do with the conventions of
calling javascript code in a .js file from an html file.
No difference.

[...] Is there
something that needs to be declared in the html file in addition to
<script src=file.js></script>? Is it necessary to remove the
<script></script> tags within the .js file because of the duplication?
No and 'WTF'? :-o

There should not be any script tags inside the .js file. They are for
the HTML file to tell the browser to create a script element. A js file
should only contain the contents of the element (in other words, don't
have any '<script...>' tags in the js file).
I've tried a few variations but haven't hit on anything that makes this
or simpler javascripts work.

Can you (or anyone else reading this) try it with a simple piece of code
and give me the relevant lines in the calling html file and the .js file
that is called?

~ J

*** Sent via Developersdex http://www.developersdex.com ***

--
Rob
Jul 23 '05 #7
>There should not be any script tags inside the .js file. They are for
the HTML file to tell the browser to create a script element. A js file
should only contain the contents of the element (in other words, don't
have any '<script...>' tags in the js file).
Rob: But you have the tags in your js file code:
'<script type="text/javascript">'
and then '</script>' at the end. These are what I referred to when I
asked:

'Is it necessary to remove the <script></script> tags within the .js
file because of the duplication?'
I have no idea where the sliding banner goes, you
didn't post the code for it.


The sliding banner code is the positionit() function. As I've said,
this works perfectly when the code is in the html file.

~ J

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #8
Jake j wrote:
There should not be any script tags inside the .js file. They are for
the HTML file to tell the browser to create a script element. A js file
should only contain the contents of the element (in other words, don't
have any '<script...>' tags in the js file).

Rob: But you have the tags in your js file code:
'<script type="text/javascript">'
and then '</script>' at the end. These are what I referred to when I
asked:

'Is it necessary to remove the <script></script> tags within the .js
file because of the duplication?'


The original script, as posted, did not work at all in any browser I
tried it in, it was missing a line of code. So I've mucked around to
get it going how I think you wanted it...

If what you mean by 'sliding banner' is that it stays at some offset
from the top of the page regardless of scrolling, and the text stays on
the right, then the following is all you need. It sets anything inside
'staticbanner' to right-align so you don't need anything to do with left
scrolling, window width or left positioning, it all happens 'cause of
CSS. Just set some right padding on the enclosed A to keep it away from
the right edge (I've used 10px).

Your banner can be any size and will just sit in the 'right' place... ha
ha. :-)

The script is hugely simplified and should use far less resources than
what was originally posted.

Note that you should be using HTML 4 strict, which affects positioning
and you must add units to style measurements (e.g. using 'px' when
setting 'crossobj.style .top').

It is tested in Firefox and IE, I can't test in old Netscape. I got the
code for scrollT() from Quirksmode, it should work in any IE or 'zilla
based browser.

<URL:http://www.quirksmode. org/index.html?/js/display.html>

If you want the script in the page, paste the contents of the script
between the script tags and remove the src attribute.

The HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><ti tle>slider play</title>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<style type="text/css">
body { margin: 0; padding: 0;}
</style>
</head>
<body>
<div style="height: 150px; background-color: #ddddff;"></div>

<!-- static slider -->
<div id="staticbanne r"
style="position :absolute; top: 120px;
width: 100%; text-align: right;">
<a href="linkpage. htm?adslider" target="_blank"
style="padding: 0 10px 0 0;">BannerTex t</a>
</div>

<!-- Insert script element -->
<script type="text/javascript" src="slider.js" ></script>

<div style="height: 1000px;">Make space for scrolling</div>

</body></html>
The script 'slider.js'

// Get a reference to 'staticbanner'
var crossobj = document.getEle mentById?
document.getEle mentById('stati cbanner'): document.all?
document.all.st aticbanner : document.static banner;

// Keeps the banner 120px from top
function positionit(){
// If browser supports style
if ( crossobj.style ) {
crossobj.style. top = scrollT() + 120 + 'px';
}
// If browser supports layers
else if ( document.layers ){
crossobj.top = scrollT() + 130 + 'px';
}
}

// Returns the number of pixels the page has scrolled down
function scrollT() {
if (self.pageYOffs et) return self.pageYOffse t;
if (document.docum entElement
&& document.docume ntElement.scrol lTop)
return document.docume ntElement.scrol lTop;
if (document.body) return document.body.s crollTop;
}

// Start the timer
setInterval('po sitionit()',200 )

--
Rob
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2616
by: steve | last post by:
I know about hotscripts, so please don’t send me there (been there)... I am looking for a site advertising script in php/mysql, which does all the regular banner rotation and management, and also allows advertisers to come in, register, upload their ad, pay with paypal, and allow me then to validate and start running their ads.
11
5589
by: Jake j | last post by:
Yes, this is a simple and straightforward thing to do as long as the .js file doesn't use a function. When I try a script with a function in a js file I can't get it to work (though it works fine within the html file). I'm guessing that there's some syntax or convention I'm not doing right. What I most need is an example. Can someone write a js file with an alert command, for example, to keep it simple. Perhaps something like: var...
4
1801
by: Jake j | last post by:
To those of you who sent me working examples of js include routines, much thanks. I see from them examples that what I'm trying to get to requires a higher level of js knowledge than I've got. Here's what I'm really trying to do: I've been using a sliding banner javascript routine in several html files and it works fine. Rather than duplicate the code in each file, however, it would obviously be preferable to including the routine in...
5
2005
by: Bjorn Sagbakken | last post by:
Hello I have just migrated from VS 2003 to VS 2005, and .NET framework 1.1 to 2.0 I am at the end of debugging and fixing stuff. Now there is one error I just cannot find a solution to: On some pages I have applied a small client-script to trap the enter-key beeing pressed and re-route this action. With the new version this little script still works, it traps the enter-key. BUT it causes postback-errors on many other .NET control,...
2
2599
by: kwenterprise | last post by:
Hello All, I am normally great at figuring out ways around iframe issues that frustrate us all. I am using javasript to try and break an iframe that I have a banner rotator embedded in but it is not working. I want visitors to click the banners and actually be taken to those pages instead of the new page being stuck in the iframe. What ever the case when I click on any banner, it opens the url right there inside the frame instead of...
3
1889
by: Faisal Shah | last post by:
As the solution.. I have got this script code.. it's an open source so i can modify it.. The problem is it's a guest book script written in very highly and deeply php language that I am not able to understand all.. BUT I am here you guys can read and help me.. From this script i would need your help, You will have to separate 2 things <PLEASE> 1. Bunch of code, Which writes message and gives a unique id to each entry...SO identified...
3
5193
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
2120
by: dragon52 | last post by:
Hi, I don't know how other people structure the banner for different screen sizes. I have done the following but don't know how to span the banner text over several sections. To adjust for screen sizes I have a small picture float to the left and another picture float to the right. There is a small gap in the middle. This is working and looking the way I want it to. Now the banner text has large font and in bold. I want to have this...
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.