JavaScript setAttribute

B

Bob Phillips

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.getAttribute(metaTag)==metaName)
break;
}
if (debug) alert('[setMetaContent] i='+i+' value '+value);
metas.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
 
R

RobG

Bob said:
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.getAttribute(metaTag)==metaName)
break;
}
if (debug) alert('[setMetaContent] i='+i+' value '+value);
metas.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.httpEquiv.toLowerCase() ){
alert(metas.content);
}
}

</script>
 
B

Bob Phillips

RobG said:
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).
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?
 
R

RobG

Bob said:
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>
 
B

Bob Phillips

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 said:
Bob said:
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>
 
T

Thomas 'PointedEars' Lahn

Bob said:
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
 
B

Bob Phillips

Thomas 'PointedEars' Lahn said:
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!
 
T

Thomas 'PointedEars' Lahn

Bob said:
^^^^


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top