Forum Moderators: open

Message Too Old, No Replies

Need a little help tweaking simple code

Apostrophes interfere with javascript

         

illtron

3:15 pm on Jul 19, 2005 (gmt 0)

10+ Year Member



Here's my situation...I've got a small bit of javascript, which I got a lot of help with on this forum previously. It works pretty much perfectly, in most cases, with one exception.

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>

RonPK

7:57 pm on Jul 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very untested, but what if you replace
var lTxt=document.links[i].text;

by
var lTxt = escape(document.links[i].text);

?

illtron

12:33 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



Well that *sort of* works. When I do that, It works, but spaces are represented as %20, quotes as %27, etc.

So if I click on a link that says "Here's the link," I'd get "Here%27s%20the%20link" as the result.

Is this a start or a dead end?

Alternative Future

12:45 pm on Jul 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What happens when you use the unescape() function in place of or after you have used the escape:

var lTxt = unescape(document.links[i].text);
or
unescape(lTxt);

-gs

illtron

2:08 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



When I do both, I get the same result as the original code -- nothing appears in the text area on those.

I should also probably clarify. When I say apostrophes, this also applies to single straight quotes, but since they're the same thing, I figure it goes without saying.

RonPK

4:43 pm on Jul 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, should have seen this before. onclick is not an attribute, it's an event. So you need to do something like this:

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.

illtron

5:08 pm on Jul 20, 2005 (gmt 0)

10+ Year Member



Excellent! That works, but can you help me with some fine-tuning now?

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?

RonPK

8:30 pm on Jul 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, there are no predefined methods to handle such operations. It takes some good old string manipulation, maybe even a regular expression...

illtron

11:14 am on Jul 21, 2005 (gmt 0)

10+ Year Member



Sounds like we're getting out of my league, unfortunately. :-/

RonPK

12:48 pm on Jul 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, getting rid of the HTML tags is not that hard - if you know your regexps ;). An example of the replace() method:

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, "");

but just
txt.replace(pattern, "");

RonPK

1:06 pm on Jul 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's another one, for truncating the link:

var txt = "http://www.domain.com/path/to/page.html";
var pattern = /^.*?:\/\/.*?(\/.*)$/;
txt.replace(pattern, "$1");
alert(txt);

Should show "/path/to/page.html"

Please note that I haven't tested these with different input...

illtron

2:57 pm on Jul 21, 2005 (gmt 0)

10+ Year Member



OK, I'm a little lost, but I think I see where you're going with this. Can you help me integrate it into the working code?

RonPK

3:35 pm on Jul 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK:

parent.topframe.document.getElementById('thebox').value = this.href.replace(/^.*?:\/\/.*?(\/.*)$/, "$1") + '\r' + this.innerHTML.replace(/<\/?.*?>/g, "");

All that should be on one line.

illtron

3:50 pm on Jul 21, 2005 (gmt 0)

10+ Year Member



Ron, I can't thank you enough. You've been extremely patient and helpful. This is exactly what I needed. The best part is, the code looks a lot better now.

I've actually managed to learn about javascript and regular expressions. Thanks again!