Hello I'm using this code I got from an example:

<head>
<TITLE>Creating a link</TITLE> 
<SCRIPT LANGUAGE="JavaScript"> 
<!--
function createLink() 
{ 
	var szURL = prompt("Enter a URL:", "http://");
	if ((szURL != null) && (szURL != "")) {
		document.execCommand("CreateLink",false,szURL);
	}
}
// --> 
</SCRIPT>
</HEAD> 
<BODY> 
<H1>Creating a link</H1> 
Here's some text. Select some and click the button to turn it into a hyperlink. 
<BR /> 
<BUTTON ONCLICK="createLink()">Click to create the link</BUTTON> 
</body>
</html>

It's working ok in IE, but in Firefox (3.5.3 I'm using), I get the following error:

uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIDOMNSHTMLDocument.execCommand]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: [url]http://localhost/america/editora/simple2.html[/url] :: createLink :: line 11" data: no]

Line 0

I have googled for this error but seems nobody is having problems with it? Any help much appreciated, I'm a rookie in javascript.

(line 11 is the CreateLink line)

Recommended Answers

All 3 Replies

Hello I'm using this code I got from an example:

<head>
<TITLE>Creating a link</TITLE> 
<SCRIPT LANGUAGE="JavaScript"> 
<!--
function createLink() 
{ 
	var szURL = prompt("Enter a URL:", "http://");
	if ((szURL != null) && (szURL != "")) {
		document.execCommand("CreateLink",false,szURL);
	}
}
// --> 
</SCRIPT>
</HEAD> 
<BODY> 
<H1>Creating a link</H1> 
Here's some text. Select some and click the button to turn it into a hyperlink. 
<BR /> 
<BUTTON ONCLICK="createLink()">Click to create the link</BUTTON> 
</body>
</html>

It's working ok in IE, but in Firefox (3.5.3 I'm using), I get the following error:

I have googled for this error but seems nobody is having problems with it? Any help much appreciated, I'm a rookie in javascript.

(line 11 is the CreateLink line)

That's because firefox is not capable of executing this or any other commands associated with document.exec !

Hmm but document.execCommand with "bold", 'underline' and 'italics' work in FF.

Anyway... if you could point me in the right direction as to how to create a link in FF I'd appreciate. Just a hint will be enough :)

commented: this is for talking nonsense - but moreover for never saying a smple "thanks". +0

Hmm but document.execCommand with "bold", 'underline' and 'italics' work in FF.

Anyway... if you could point me in the right direction as to how to create a link in FF I'd appreciate. Just a hint will be enough :)

For as far as I know it doesn't,
where did you see that? :')
[in firefox it's only possible in recently adopted 1997 IE technology "contentEditable" mode turned On, not in normal view mode. ]

No, I can't point you to any direction or give you a hint or anything of that sort

But what I can do, is to give you my original code that will also work in FX right?
:')

(FX, Opera and IE tested, -please test it in other browsers and report any eventual problems you might find).

Here is the code:

<html>
<head>
<title>Text to Hyperlink</title> 
</head> 
<body> 
<h1>Create a link</h1> 
Select some text and click the button. The selected text will become the named hyperlink<br>
My Homepage<br>
My Favorite<br>
My Search Page<br><br>
<button onclick="createLink(event)">Make it a link</button> 
<script>
function createLink(e){//b.b. Troy III p.a.e.
 if(e.target){ //get ATL
	var a = window.getSelection().getRangeAt(0);
	var b = a.toString();  
	var z = document.createElement("span");
	var l2 = prompt("Enter URL:", "http://");
		b = b.link(l2);
		z.innerHTML=b;
		a.deleteContents();
		a.insertNode(z) }
 else{ //get the ATW
        document.execCommand("CreateLink") }
}
</script>
</body>
</html>

Note: to shorten the ATL method and gain some efficiency, I've used some widely compatible DOM0,1 methods.

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.