473,698 Members | 2,631 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

alert inside <DIV>

joe
Hi,

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

in my .html file, after an Ajax query my weberserver sends:
<script language="JavaS cript">
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 4022
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="JavaS cript">
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(s tringToExecute) {
var newScript = document.create Element('script ');
newScript.type = "text/javascript";
newScript.text = stringToExecute ;
document.getEle mentsByTagName( 'head')[0].appendChild(ne wScript);
}

If you absolutely need it in the div element, change it to
document.getEle mentById('someD iv').appendChil d(newScript)

And call it as such:

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

or:

executeJSCode(s tringOfCode)

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.javas cript 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(s tringToExecute) {
var newScript = document.create Element('script ');
newScript.type = "text/javascript";
newScript.text = stringToExecute ;
document.getEle mentsByTagName( 'head')[0].appendChild(ne wScript);
}

If you absolutely need it in the div element, change it to
document.getEle mentById('someD iv').appendChil d(newScript)

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.

doSomethingToIn cludeScript();
var nDiv = document.getEle mentById( "divScriptTarge t" );
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(s tringToExecute) {
var newScript = document.create Element('script ');
newScript.ty pe = "text/javascript";
newScript.te xt = stringToExecute ;
document.getEl ementsByTagName ('head')[0].appendChild(ne wScript);
}

If you absolutely need it in the div element, change it to
document.getEl ementById('some Div').appendChi ld(newScript)

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:

doSomethingToIn cludeScript();
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.getEle mentById('someD iv').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(s tringToExecute) {
var newScript = document.create Element('script ');
newScript.ty pe = "text/javascript";
newScript.te xt = stringToExecute ;
document.getEl ementsByTagName ('head')[0].appendChild(ne wScript);
}

If you absolutely need it in the div element, change it to
document.getEl ementById('some Div').appendChi ld(newScript)

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>Executin g Script blocks inserted via innerHTML</title>
<script type="text/javascript">
function loadHTMLFragmen t(someFragment) {
document.getEle mentById('myDiv ').innerHTML = '<br>' + someFragment;
//notes below on that br element
var d = document.getEle mentById('myDiv ').getElementsB yTagName("scrip t");
var t = d.length;
for (var x=0;x<t;x++){;
var newScript = document.create Element('script ');
newScript.type = "text/javascript";
newScript.text = d[x].text;
document.getEle mentById('myDiv ').appendChild( newScript);
}
}
</script>
<body>
<div id="myDiv"></div>
<textarea onchange="loadH TMLFragment(thi s.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 <scriptelemen t,
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.javas cript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3494
by: Xah Lee | last post by:
with strict HTML spec, can one have <p> tags inside table's <td> tag? also, in strict XHTML, can one have <p> tages inside <div>? Thanks. Xah xah@xahlee.org ∑ http://xahlee.org/
3
14403
by: Joseph Haig | last post by:
I have a style sheet containing the following definitions: #leftcontent { position: absolute; left:20px; top:60px; width:180px; border:1px solid #000; padding-top: 15px; }
8
5144
by: slim | last post by:
hi again all, i am still working on the website as mentioned in earlier threads and have hit another snag... http://awash.demon.co.uk/index.php http://awash.demon.co.uk/vd.css the php is pulling a name and placing it under the thumbnail (the text is
1
2109
by: Speedy | last post by:
I have the following html body implemented in a site. But as soon as I added the <div> tag the <a> is not function any longer... Can someone tell me why this is not working ? Cheers Speedy <body> <div id="text"> <h2>faq</h2>
9
3140
by: Julia Briggs | last post by:
How do I construct a <iframe> or equivalent for FireFox/NS browsers, inside a screen centered <div> tag? Can it be done?
4
5548
by: Alan Silver | last post by:
Hello, Please pardon what is probably a dumb question, but if I have HTML like this... <div> <h3>Ferrets</h3> </div> and CSS like this...
5
39005
by: chakradhari.ashish | last post by:
Does anybody know how can I load an html page inside a <div</div> where the content gets update on the onclick even of anchor <a href=> </a>? I googled around and followed some links pointing me that it is possible to use javascript combined with CSS for doing this? Any ideas? Chakra
5
5259
nathj
by: nathj | last post by:
Hi All, I'm working on a new site that has a two column layout underneath a title bar. If you check out: http://www.christianleadership.org.uk/scratch/mainpage.php using IE or Opera you will see what I am after as these browsers work fine. However, in FF the results are slightly different - take a look and you'll see that the <p>, within <div class="mainContent"> has shot way over to the right. I've been working on this for 2days now...
8
10042
prino
by: prino | last post by:
Hi all, I've written code (in REXX) that takes files in legacy languages (PL/I, COBOL, z/OS assembler, etc) and converts them into HTML in a format similar to what's displayed in the z/OS ISPF editor. A fellow member of the PCG has helped me by creating a bit of Javascript to emulate the scrolling and using Google I've now gotten it into a state where it almost passes the W3C Markup Validation Service. However, the one error, Error Line 166,...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.