469,290 Members | 1,882 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

JavaScript setAttribute

Hope someone can help.

I am struggling to write an meta element in JS. The meta element in question
is the refresh element.

This is the code I am using

//--------------------------------------------------------------------------
----
function setMetaContent(metaTag, metaName, value) {
//--------------------------------------------------------------------------
----
var metas = document.getElementsByTagName('meta');

if (debug) alert('[setMetaContent] metaTag='+metaTag+' metaName=
'+metaName+' value '+value);
for (var i=0; i<metas.length; i++) {
if (metas[i].getAttribute(metaTag)==metaName)
break;
}
if (debug) alert('[setMetaContent] i='+i+' value '+value);
metas[i].setAttribute('content',value);
}
and I call it with

var metaValue = '10;url='+rootSource+fileBase+aryChile[nImgId][4]+'.html';
if (debug) alert('[writeIMG] metaValue ='metaValue);
setMetaContent('http-equiv','refresh',metaValue);

where rootSource = "../source/", fileBase = "picture", and
aryChile[nImgId][4] resolves to an integer . The url setup looks okay, but
the code seems to disappear intgo oblivion.

Two questions
1) can I set a meta element in this way
2) does the code look correct?

TIA

Bob
Jul 23 '05 #1
7 19065
Bob Phillips wrote:
Hope someone can help.

I am struggling to write an meta element in JS. The meta element in question
is the refresh element.

This is the code I am using

//--------------------------------------------------------------------------
----
function setMetaContent(metaTag, metaName, value) {
//--------------------------------------------------------------------------
----
var metas = document.getElementsByTagName('meta');

if (debug) alert('[setMetaContent] metaTag='+metaTag+' metaName=
'+metaName+' value '+value);
for (var i=0; i<metas.length; i++) {
if (metas[i].getAttribute(metaTag)==metaName)
break;
}
if (debug) alert('[setMetaContent] i='+i+' value '+value);
metas[i].setAttribute('content',value);
}
and I call it with

var metaValue = '10;url='+rootSource+fileBase+aryChile[nImgId][4]+'.html';
if (debug) alert('[writeIMG] metaValue ='metaValue);
setMetaContent('http-equiv','refresh',metaValue);

where rootSource = "../source/", fileBase = "picture", and
aryChile[nImgId][4] resolves to an integer . The url setup looks okay, but
the code seems to disappear intgo oblivion.

Two questions
1) can I set a meta element in this way
I doubt it. You appear to be trying to modify an external file, if so,
then absolutely not (unless you are using some IE extension, HTA, etc.
that allows it).

2) does the code look correct?


No. The 'http-equiv' property of a meta tag is called 'httpEquiv'
(ignoring the external file access issue).

I'm not sure that modifying the value of the refresh property of meta
element has any effect. Seems to me that once the browser has parsed
it on page load, modifying it doesn't change the refresh rate.

Here's a little script to see what the refresh rate is:

<script type="text/javascript">

var metas = document.getElementsByTagName('meta');
for (var i=0; i<metas.length; i++){
if ( 'refresh' == metas[i].httpEquiv.toLowerCase() ){
alert(metas[i].content);
}
}

</script>

--
Rob
Jul 23 '05 #2

"RobG" <rg***@iinet.net.auau> wrote in message
news:M_*****************@news.optus.net.au...

I doubt it. You appear to be trying to modify an external file, if so,
then absolutely not (unless you are using some IE extension, HTA, etc.
that allows it).
I am not trying to modify a file, just change the URL that the refresh will
redirect to. The background is that I have a slideshow, and it automatically
redirects after 10 secs. Now there are 300 items in this slideshow, and as
each redirects to a different page, that is 300 HTMl pages to maintain. I
hoped to do it by one page, replicated, that got its information at run time
to determine the redirect page (I can't use server side code, has to be
client on this).
2) does the code look correct?


No. The 'http-equiv' property of a meta tag is called 'httpEquiv'
(ignoring the external file access issue).

I'm not sure that modifying the value of the refresh property of meta
element has any effect. Seems to me that once the browser has parsed
it on page load, modifying it doesn't change the refresh rate.


As I say, the rate is of no interest, just the URL, and I am doing this as
it loads.

Any other way to get what I need?
Jul 23 '05 #3
Bob Phillips wrote:
"RobG" <rg***@iinet.net.auau> wrote in message
news:M_*****************@news.optus.net.au...
I doubt it. You appear to be trying to modify an external file, if so,
then absolutely not (unless you are using some IE extension, HTA, etc.
that allows it).

I am not trying to modify a file, just change the URL that the refresh will
redirect to.


The HTML 4 spec says:

"Note. Some user agents support the use of META to refresh the
current page after a specified number of seconds, with the option
of replacing it by a different URI. Authors should not use this
technique to forward users to different pages, as this makes the
page inaccessible to some users. Instead, automatic page forwarding
should be done using server-side redirects."

So you shouldn't be using a meta element for this.
redirect to. The background is that I have a slideshow, and it automatically
redirects after 10 secs. Now there are 300 items in this slideshow, and as
each redirects to a different page, that is 300 HTMl pages to maintain. I
hoped to do it by one page, replicated, that got its information at run time
to determine the redirect page (I can't use server side code, has to be
client on this).

[...]
As I say, the rate is of no interest, just the URL, and I am doing this as
it loads.

Any other way to get what I need?

Why not use setTimeout to change document.location.href at your chose
interval? The 'slideshow' won't work for non-JavaScript browsers,
whether that suits or not is up to you.

Put the following into an external file called 'changeURL.js', put
the HTML into files called 0.html and 1.html. It will go between
them every 2 seconds. Change as required.

I've only been able to test this in Safari, so test thoroughly.
/******* changeURL.js *******/

/* changeURL
* Changes files from 0.html to 'numfiles'.html
* at intervals of 'lag'
*/
function changeURL() {
var numFiles = 2;
var b = String(document.URL);

// Get filename of current page
var f = b.replace(/.*[/\\\\]/,'');

// Increment the filename & build new name
var g = f.split('.');
g = (+g[0] + 1)%numFiles + '.' + g[1];

// Change the URL
document.location.href = b.replace(f,'') + g;

}

setTimeout("changeURL()",2000);
/******* 0.html *******/

<html>
<head><title>Slide show 0</title>

<script type="text/javascript" src="changeURL.js"></script>

</head>
<body>
<h1>Page 0</h1>
</body>
</html>

/******* 0.html *******/

<html>
<head><title>Slide show 1</title>

<script type="text/javascript" src="changeURL.js"></script>

</head>
<body>
<h1>Page 1</h1>
</body>
</html>

--
Rob
Jul 23 '05 #4
Rob,

Thanks for that, the suggestion looks interesting, and I have tried to
implement a modified version, but I have a problem.

What I am doing is to work out the next page to load and storing it into a
variable called sNextFile, and setting a timer like so

setTimeout("changeURL('"+sNextFile+"')",5000);

Here is my changeURL routine, very simple

//--------------------------------------------------------------------------
----
function changeURL(inURL) {
//--------------------------------------------------------------------------
----

if (debug) alert('[changeURL] '+inURL)
document.location.href = inURL;

}

Problem is that when I pass a value to the function, it strips the / from
the value. So although I have the value

file://D:\Bob\web\nahrum\source\picture2.html

but in the function, the value seen is

file://D:Bobwebnahrumsourcepicture2.html

so of course the page is not found. Any idea what is going on?

TIA

Bob

"RobG" <rg***@iinet.net.auau> wrote in message
news:42**********************@per-qv1-newsreader-01.iinet.net.au...
Bob Phillips wrote:
"RobG" <rg***@iinet.net.auau> wrote in message
news:M_*****************@news.optus.net.au...
I doubt it. You appear to be trying to modify an external file, if so, then absolutely not (unless you are using some IE extension, HTA, etc.
that allows it).

I am not trying to modify a file, just change the URL that the refresh will redirect to.


The HTML 4 spec says:

"Note. Some user agents support the use of META to refresh the
current page after a specified number of seconds, with the option
of replacing it by a different URI. Authors should not use this
technique to forward users to different pages, as this makes the
page inaccessible to some users. Instead, automatic page forwarding
should be done using server-side redirects."

So you shouldn't be using a meta element for this.
redirect to. The background is that I have a slideshow, and it automatically redirects after 10 secs. Now there are 300 items in this slideshow, and as each redirects to a different page, that is 300 HTMl pages to maintain. I hoped to do it by one page, replicated, that got its information at run time to determine the redirect page (I can't use server side code, has to be
client on this).


[...]
As I say, the rate is of no interest, just the URL, and I am doing this as it loads.

Any other way to get what I need?

Why not use setTimeout to change document.location.href at your chose
interval? The 'slideshow' won't work for non-JavaScript browsers,
whether that suits or not is up to you.

Put the following into an external file called 'changeURL.js', put
the HTML into files called 0.html and 1.html. It will go between
them every 2 seconds. Change as required.

I've only been able to test this in Safari, so test thoroughly.
/******* changeURL.js *******/

/* changeURL
* Changes files from 0.html to 'numfiles'.html
* at intervals of 'lag'
*/
function changeURL() {
var numFiles = 2;
var b = String(document.URL);

// Get filename of current page
var f = b.replace(/.*[/\\\\]/,'');

// Increment the filename & build new name
var g = f.split('.');
g = (+g[0] + 1)%numFiles + '.' + g[1];

// Change the URL
document.location.href = b.replace(f,'') + g;

}

setTimeout("changeURL()",2000);
/******* 0.html *******/

<html>
<head><title>Slide show 0</title>

<script type="text/javascript" src="changeURL.js"></script>

</head>
<body>
<h1>Page 0</h1>
</body>
</html>

/******* 0.html *******/

<html>
<head><title>Slide show 1</title>

<script type="text/javascript" src="changeURL.js"></script>

</head>
<body>
<h1>Page 1</h1>
</body>
</html>

--
Rob

Jul 23 '05 #5
Bob Phillips wrote:
Problem is that when I pass a value to the function, it strips the / from
the value. So although I have the value

file://D:\Bob\web\nahrum\source\picture2.html
The proper URI is

file:///D:/Bob/web/nahrum/source/picture2.html

which is the same as

file://localhost/D:/Bob/web/nahrum/source/picture2.html
but in the function, the value seen is

file://D:Bobwebnahrumsourcepicture2.html

so of course the page is not found. Any idea what is going on?
The backslash (`\') acts as escape character in string and RegExp
literals. You have to escape it to have it displayed: \\ (nothing new)
[Top post]


<http://jibbering.com/faq/>
PointedEars
Jul 23 '05 #6

"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:32****************@PointedEars.de...
Bob Phillips wrote:
Problem is that when I pass a value to the function, it strips the / from the value. So although I have the value

file://D:\Bob\web\nahrum\source\picture2.html


The proper URI is

file:///D:/Bob/web/nahrum/source/picture2.html


But I get that value by issuing a

var thisURL = String(document.URL);

in the JS!
Jul 23 '05 #7
Bob Phillips wrote:
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote [...]:
Bob Phillips wrote:
> Problem is that when I pass a value to the function, it strips the / from

^^^^
Please follow the recommendations at <http://insideoe.tomsterdam.com/>
to avoid further borken quotes.
> the value. So although I have the value
>
> file://D:\Bob\web\nahrum\source\picture2.html


The proper URI is

file:///D:/Bob/web/nahrum/source/picture2.html


But I get that value by issuing a

var thisURL = String(document.URL);


The value of `document.URL' is already a string, it does not need to be
"stringified". Furthermore, you should use `location' or `location.href'
instead; `document.location' and its IE version `document.URL' is
deprecated long since. And if even that will not help, you should
convert backslashes to forward slashes before you continue:

var thisURL = location.href.replace(/\\/g, "/");
PointedEars
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by relaxedrob | last post: by
2 posts views Thread by sorobor | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.