Connecting Tech Pros Worldwide Help | Site Map

alert inside <DIV>

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 16th, 2006, 01:15 PM
joe
Guest
 
Posts: n/a
Default alert inside <DIV>

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?


  #2  
Old November 16th, 2006, 07:15 PM
Randy Webb
Guest
 
Posts: n/a
Default Re: alert inside <DIV>

joe said the following on 11/16/2006 9:38 AM:
Quote:
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.
Quote:
<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.
Quote:
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/
  #3  
Old November 20th, 2006, 03:45 PM
joe
Guest
 
Posts: n/a
Default Re: alert inside <DIV>


Randy Webb schrieb:

Quote:
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>"
Quote:
which should be included in the div-block.
>
And it gets included. Alert the innerHTML and it is there.
how?

Quote:
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:
Quote:
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.

  #4  
Old November 20th, 2006, 04:25 PM
Laurent Bugnion
Guest
 
Posts: n/a
Default Re: alert inside <DIV>

Hi,

joe wrote:
Quote:
Randy Webb schrieb:
>
>
Quote:
>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.
Quote:
Quote:
>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 );
Quote:
Quote:
>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:
>
Quote:
>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
  #5  
Old November 20th, 2006, 06:25 PM
Randy Webb
Guest
 
Posts: n/a
Default Re: alert inside <DIV>

joe said the following on 11/20/2006 12:08 PM:
Quote:
Randy Webb schrieb:
>
>
Quote:
>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>
Quote:
Quote:
>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.
Quote:
Quote:
>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:
>
Quote:
>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/
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.