473,408 Members | 2,405 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,408 software developers and data experts.

Replacing href text

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

Similar topics

5
by: Sorby | last post by:
Hi I've been coding in PHP for a little while now and was starting to feel pretty confident but then realise I need to understand regular expressions to solve a particular problem I've got ......
8
by: jamesfin | last post by:
Dear XMLers, Please guide me... I have a simple xml file... <URLTest>
1
by: Martijn Berendsen | last post by:
Hello! Example: <a href="www.abc.com">ABC</a> <a href="www.xyz.com">www.xyz.com</a> I'm looking for a script that replaces the part within the HTML tag (www.abc.com and www.xyz.com) with...
14
by: Adnan Siddiqi | last post by:
Hi Suppose I have following URLs comming from an HTML document <a href="http://mydomain1.com">Domain1</a> <a...
2
by: David | last post by:
Sent this to alt.php a couple of days back, but doesn't look like I'll get an answer, so trying here. I'm trying to convert a script to use friendly URLs, I've done this before, but my PHP...
1
by: gViscardi | last post by:
Hello all, Ok, so what I am attempting to accomplish is to replace my table-based website with a CSS layout site, but I have run into a few hitches. My main problem is that currently my website...
1
by: cma6 | last post by:
I have a page which uses tables for layout http://www.vintagetextile.com/1920s_to_1930s.htm The checkerboard pattern is achieved with this markup: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML...
1
by: Ivann | last post by:
Hi All, I have a javascript/css menut that i have which i start when the html page loads. <script type="text/javascript">cssdropdown.startchrome("chromemenu")</ script> ***THIS IS IN THE...
1
by: TamusJRoyce | last post by:
I have xsl code which I was hoping could be used to replace one specific tag from an xhtml document and output another xhtml document. xsl has phenomenal potential in data replacing, but coming...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.