By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Replacing href text

P: n/a
I have a page that contains several HREFs. Each HREF that I'm
interested in has a common parameter (parmX). Does anyone have a script
example on how to find-and-replace parmX with parmY?

I'm assuming that I'd need to do this in the onload event - but I don't
know much about Javascript.

Please Help!

Thanks
Glenn

Jan 26 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
wrote on 26 jan 2006 in comp.lang.javascript:
I have a page that contains several HREFs. Each HREF that I'm
interested in has a common parameter (parmX). Does anyone have a script
example on how to find-and-replace parmX with parmY?

I'm assuming that I'd need to do this in the onload event - but I don't
know much about Javascript.


<a href='...'>...</a> usually contains just a URL.

Do you mean that?

Show us some code, the relevant part, that is.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 26 '06 #2

P: n/a
Yes - thats what I mean. The URL in the <a Href= tag may contain a
parameter &amp;stream=1. I want to replace every occurrence of that
with a new parameter &amp;stream=0.

I wrote a test script:

OnLoad() {
var frm = document.forms[0];
var cnt = 0;

// loop through all elements
for (i=0; i<frm.length; i++) {

// Look for hrefs with the stream=1 parameter
if (frm.elements[i].name.indexOf('&amp;stream=1') != -1) {
cnt = cnt + 1;
alert('Found HREF w/stream=1[' + cnt + ']');
}
}
}
return;
}

Followed by a call to the function in the Body Onload event

</head>

<body onload="OnLoad();" marg...

However, I'm getting an error - so I assume that my syntax or something
is wrong. I just wanted to start trying to access the function and
report on hits. I'll have to tackle the data replacement once I get
over this hurdle...

Thanks
Glenn

Jan 26 '06 #3

P: n/a
go****@nixonpeabody.com wrote:
Yes - thats what I mean.
What is what you mean?
The URL in the <a Href= tag may contain a parameter &amp;stream=1. I want to replace every occurrence of that
with a new parameter &amp;stream=0.

I wrote a test script:

OnLoad() { Bad name for a function

function searchHrefForStr(Str){ var frm = document.forms[0]; Surely you mean "document.links" ?

var cnt = 0;

// loop through all elements
for (i=0; i<frm.length; i++) { for(var i=0;i<document.links.length;i++){


// Look for hrefs with the stream=1 parameter
if (frm.elements[i].name.indexOf('&amp;stream=1') != -1) { if (document.links[i].href.indexOf(Str) != -1{

cnt = cnt + 1; Not neccesary


alert('Found HREF w/stream=1[' + cnt + ']'); alert('Found HREF w/'+Str'+'[' + (cnt++) + ']');
}
}
}
return; why return? }

Followed by a call to the function in the Body Onload event

</head>

<body onload="OnLoad();" marg... <body onload="searchHrefForStr('stream=1');"
Not tested.
Mick

However, I'm getting an error - so I assume that my syntax or something
is wrong. I just wanted to start trying to access the function and
report on hits. I'll have to tackle the data replacement once I get
over this hurdle...

Thanks
Glenn

Jan 26 '06 #4

P: n/a
mick white wrote on 26 jan 2006 in comp.lang.javascript:
go****@nixonpeabody.com wrote:
Yes - thats what I mean.


What is what you mean?
The URL in the <a Href= tag may contain a
parameter &amp;stream=1. I want to replace every occurrence of that
with a new parameter &amp;stream=0.

[...........]


Let's make this compact:

==========================
<script type='text/javascript'>
hrefChange(){
for( h in document.links )
h.href=h.href.replace(/[\?\&]stream=0(\&||$)/,'$1stream=1$2')
}
</script>

<body onload='hrefChange()'>
.....
==========================

Not tested
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 26 '06 #5

P: n/a
Evertjan. wrote:
mick white wrote on 26 jan 2006 in comp.lang.javascript:
go****@nixonpeabody.com wrote:

Yes - thats what I mean.


What is what you mean?
The URL in the <a Href= tag may contain a
parameter &amp;stream=1. I want to replace every occurrence of that
with a new parameter &amp;stream=0.


[...........]

Let's make this compact:

==========================
<script type='text/javascript'>
hrefChange(){
for( h in document.links )
h.href=h.href.replace(/[\?\&]stream=0(\&||$)/,'$1stream=1$2')
}
</script>

<body onload='hrefChange()'>
....
==========================

Not tested

Very good, not sure that will replace "&amp;" though.
Mick
Jan 26 '06 #6

P: n/a
mick white wrote on 26 jan 2006 in comp.lang.javascript:
Evertjan. wrote:
mick white wrote on 26 jan 2006 in comp.lang.javascript:
go****@nixonpeabody.com wrote:
Yes - thats what I mean.

What is what you mean?
The URL in the <a Href= tag may contain a

parameter &amp;stream=1. I want to replace every occurrence of that
with a new parameter &amp;stream=0.

[...........]

Let's make this compact:

==========================
<script type='text/javascript'>
hrefChange(){
for( h in document.links )
h.href=h.href.replace(/[\?\&]stream=0(\&||$)/,'$1stream=1$2')
}
</script>

<body onload='hrefChange()'>
....
==========================

Not tested

Very good, not sure that will replace "&amp;" though.


If you are not sure, why not test it, it's your code anyway.

&amp; should not be necessary in the result, so try:

/[\?\&](amp;)?stream=0(\&||$)/
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 26 '06 #7

P: n/a
Evertjan. wrote:
mick white wrote on 26 jan 2006 in comp.lang.javascript:

Evertjan. wrote:

mick white wrote on 26 jan 2006 in comp.lang.javascript:
go****@nixonpeabody.com wrote:

>Yes - thats what I mean.

What is what you mean?
The URL in the <a Href= tag may contain a
>parameter &amp;stream=1. I want to replace every occurrence of that
>with a new parameter &amp;stream=0.

[...........]
Let's make this compact:

==========================
<script type='text/javascript'>
hrefChange(){
for( h in document.links )
h.href=h.href.replace(/[\?\&]stream=0(\&||$)/,'$1stream=1$2')
}
</script>

<body onload='hrefChange()'>
....
==========================

Not tested

Very good, not sure that will replace "&amp;" though.

If you are not sure, why not test it, it's your code anyway.


It's not my code...
Mick
&amp; should not be necessary in the result, so try:

/[\?\&](amp;)?stream=0(\&||$)/

Jan 26 '06 #8

P: n/a
Evertjan -

Thanks for the help! I really appreciate the code example.

I'll test it today and let you know it works.

Glenn

Jan 27 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.