469,913 Members | 2,475 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

alert inside <DIV>

joe
Hi,

I defined:
<div id="abc">
</div>

in my .html file, after an Ajax query my weberserver sends:
<script language="JavaScript">
alert('Hello World!");
</script>

which should be included in the div-block.

But the message box doesnt appear.
If i send just pure text the text is shown in the browser.

How is it possible to call a Meessage box inside a div block?

Nov 16 '06 #1
4 3757
joe said the following on 11/16/2006 9:38 AM:
Hi,

I defined:
<div id="abc">
</div>

in my .html file, after an Ajax query my weberserver sends:
Fix your server so it sends a proper script element. language is
deprecated, use the type attribute instead.
<script language="JavaScript">
alert('Hello World!");
</script>

which should be included in the div-block.
And it gets included. Alert the innerHTML and it is there.
But the message box doesnt appear.
If i send just pure text the text is shown in the browser.

How is it possible to call a Meessage box inside a div block?
Script elements that are inserted via innerHTML do not, with two
exceptions[1], get executed. You will have to parse out the script block
itself, and then use createElement to create a script block. I can't
find the particular thread where this was discussed, in depth, but if
you search the archives for "Randy Webb Dynamic loadJSFile" you can get
some hits where you can read a lot about it. Basically, you would find
the script elements in your HTMLFragment, then call a function like this:

function executeJSCode(stringToExecute){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = stringToExecute;
document.getElementsByTagName('head')[0].appendChild(newScript);
}

If you absolutely need it in the div element, change it to
document.getElementById('someDiv').appendChild(new Script)

And call it as such:

executeJSCode('String of code here without script tags')

or:

executeJSCode(stringOfCode)

Where stringOfCode is a variable that holds your script block's text. Be
aware though that if your code has a document.write statement in it then
it is not going to work properly (It will wipe out the page).

[1]
Exception 1: IE will execute script inserted via innerHTML if the script
element has the DEFER attribute.
Exception 2: NS6 (very early versions) would execute script inserted via
innerHTML

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 16 '06 #2
joe

Randy Webb schrieb:

Fix your server so it sends a proper script element. language is
deprecated, use the type attribute instead.
ok. now the server sends:
<script type="text/javascript">
alert(Executed");
</script>"
which should be included in the div-block.

And it gets included. Alert the innerHTML and it is there.
how?

Script elements that are inserted via innerHTML do not, with two
exceptions[1], get executed. You will have to parse out the script block
itself, and then use createElement to create a script block. Basically, you would find
the script elements in your HTMLFragment, then call a function like this:
function executeJSCode(stringToExecute){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = stringToExecute;
document.getElementsByTagName('head')[0].appendChild(newScript);
}

If you absolutely need it in the div element, change it to
document.getElementById('someDiv').appendChild(new Script)

And call it as such:

executeJSCode('String of code here without script tags')
its not invoked, and i would be very surprised if it would, cause its a
javascript function like alert.

Nov 20 '06 #3
Hi,

joe wrote:
Randy Webb schrieb:

>Fix your server so it sends a proper script element. language is
deprecated, use the type attribute instead.

ok. now the server sends:
<script type="text/javascript">
alert(Executed");
</script>"
That's better.
>which should be included in the div-block.

And it gets included. Alert the innerHTML and it is there.

how?
Randy means that if you include the script block in the DIV, and then
you get the DIV by ID and alert the innerHTML property, you will see
that the script was indeed included.

doSomethingToIncludeScript();
var nDiv = document.getElementById( "divScriptTarget" );
alert( nDiv.innerHTML );
>Script elements that are inserted via innerHTML do not, with two
exceptions[1], get executed. You will have to parse out the script block
itself, and then use createElement to create a script block. Basically, you would find
the script elements in your HTMLFragment, then call a function like this:
>function executeJSCode(stringToExecute){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = stringToExecute;
document.getElementsByTagName('head')[0].appendChild(newScript);
}

If you absolutely need it in the div element, change it to
document.getElementById('someDiv').appendChild(ne wScript)

And call it as such:

executeJSCode('String of code here without script tags')

its not invoked, and i would be very surprised if it would, cause its a
javascript function like alert.
What Randy means is that after including your script code (which should
be contained in a method), you must invoke the method yourself.

Something like:

SERVER SENDS:

<script type="text/javascript">
function alertExecuted()
{
alert( "Executed" );
}
</script>

CLIENT SCRIPT:

doSomethingToIncludeScript();
alertExecuted();

The script won't be executed just because it's included (except in the 2
exceptions listed by Randy). You must trigger it yourself.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Nov 20 '06 #4
joe said the following on 11/20/2006 12:08 PM:
Randy Webb schrieb:

>Fix your server so it sends a proper script element. language is
deprecated, use the type attribute instead.

ok. now the server sends:
<script type="text/javascript">
alert(Executed");
</script>"
I hope that was a typo as the alert doesn't have an opening " and you
have an errant " after the script tag:

<script type="text/javascript">
alert("Executed");
</script>
>which should be included in the div-block.

And it gets included. Alert the innerHTML and it is there.

how?
In FF2.0 and Opera 9, you can alert
document.getElementById('someDiv').innerHTML and see the code you just
put into it. With IE7, it seems that if the only thing going in is a
script block then it blows up and forgets about it. See the test page at
the bottom, along with a solution.
>Script elements that are inserted via innerHTML do not, with two
exceptions[1], get executed. You will have to parse out the script block
itself, and then use createElement to create a script block. Basically, you would find
the script elements in your HTMLFragment, then call a function like this:
>function executeJSCode(stringToExecute){
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = stringToExecute;
document.getElementsByTagName('head')[0].appendChild(newScript);
}

If you absolutely need it in the div element, change it to
document.getElementById('someDiv').appendChild(ne wScript)

And call it as such:

executeJSCode('String of code here without script tags')

its not invoked, and i would be very surprised if it would, cause its a
javascript function like alert.
Did you test it with a valid script statement? The above code works.
Even in IE. The problem comes elsewhere trying to read those script
elements. I didn't post code to do it (I did this time) to "parse out
the script block itself".

Create a test page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<title>Executing Script blocks inserted via innerHTML</title>
<script type="text/javascript">
function loadHTMLFragment(someFragment){
document.getElementById('myDiv').innerHTML = '<br>' + someFragment;
//notes below on that br element
var d = document.getElementById('myDiv').getElementsByTagN ame("script");
var t = d.length;
for (var x=0;x<t;x++){;
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = d[x].text;
document.getElementById('myDiv').appendChild(newSc ript);
}
}
</script>
<body>
<div id="myDiv"></div>
<textarea onchange="loadHTMLFragment(this.value)" rows="15"
cols="80"></textarea>
</body>
</html>

Open that page and paste this snippet into the textarea:

<script type="text/javascript">
alert("Executed");
</script>

And then blur the text area. In Opera9 and FF2.0 you get that alert.

In IE7, it seems that if the very first element is a <scriptelement,
then it won't execute it and gives an error that .text is undefined.
But, adding a second <scriptblock it works and executes both. Adding
any other HTML element before the <scriptblock makes it work
flawlessly whether there is one or more script elements. So, I added
that leading <brtag in the code above and it seems to have satisfied IE.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 20 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Xah Lee | last post: by
3 posts views Thread by Joseph Haig | last post: by
1 post views Thread by Speedy | last post: by
4 posts views Thread by Alan Silver | last post: by
5 posts views Thread by chakradhari.ashish | last post: by
nathj
5 posts views Thread by nathj | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.