473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 19283
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: relaxedrob | last post by:
Howdy All! I am really stuck with this one - I want to completely create a table within JavaScript and insert it into the document, with onMouseOut and onMouseOver handlers in the table rows. ...
4
by: rick | last post by:
The following basic script works fine in firefox by not in IE. Can anyone spot the problem? In IE I can only delete the first line but not the lines created by javascript. Also, look at the HTML...
4
by: Adam Smith | last post by:
Hello, How can I call or trigger an external javascript twice in a form? I have <script language="JavaScript" src="country_state.js" name="Country_State"> <script type="text/javascript"...
1
by: sp | last post by:
Has anyone had an issue creating usemaps for images using Javascript? The problem I've been having is related to this code: if(this.topImagePath){ this.titleTopImage =...
3
by: modeep | last post by:
I have written some code to create a table using javscript it works fine in firefox but does not show the table in IE6 // JavaScript Document var easy =...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
1
by: wenijah | last post by:
Hi everyone! First thank you for reading this post and yes, you probably already see that kind of topic title somewhere but the problem I've got today might be different than the 100 topics I've...
3
by: nigelesquire | last post by:
Please help! I'm trying to clone and delete multiple rows with JavaScript. I need two delete buttons that work...! I only have one for now, but it's not working properly, the output count is...
8
by: omlac | last post by:
im creating controls using javascript, but whenever there is a postback on the page all the controls created in javascript disappear. How do i fix this problem. the code below shows how im creating...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.