Forum Moderators: open
Basically what the script does is take the url and text of a link in one frame and put them into a textarea in another frame.
The problem arises when the link contains an apostrophe. When there's an apostrophe, the script just doesn't work.
Is there a way to tell the script to handle the character?
I don't have any control over the actual text of the links, since they're coming from a CMS.
<script type="text/javascript">function addClick(){
for (i=0;i<document.links.length;i++){
var hHref=document.links[i].getAttribute("href")
var lTxt=document.links[i].text;
document.links[i].setAttribute("onclick","intercept('"+hHref+"','"+lTxt+"')");
}
}
function intercept(hRef,lTxt){
parent.topframe.document.getElementById('thebox').value=hRef+'\r'+lTxt;
}
</script>
function addClick(){
for (i=0;i<document.links.length;i++){
document.links[i].onclick = intercept;
}
}
function intercept(){
parent.topframe.document.getElementById('thebox').value = this.href + '\r' + this.innerHTML;
}
In your function intercept() you refer to the link with this: this.href for the link, this.innerHTML for the text.
The result I get in the textarea is now this:
[domain.com...]
<b>this is the link text</b>
Previously (on the links without apostrophes and quotes), I got this result:
/path/to/page.html
this is the link text
Unfortunately, the links are always going to have that bold (due to the CMS, so it's beyond my control).
Can We strip HTML or is there a way we can do this.text or this.content, or something along those lines? Obviously those are both imaginary, but does javascript provide for this, or can it be defined?
Also, it may have been just luck for the first part so that the domain didn't show up for the link. Is it possible to strip that part so that it appeared as it originally did?
var txt = "<b>this is the link text</b>";
var pattern = /<\/?.*?>/g;
txt.replace(pattern, "");
alert(txt);
Should show an alert box with "this is the link text".
Note that the replace() method works directly on the string, ie you don't need to write
txt = txt.replace(pattern, ""); txt.replace(pattern, "");