473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question on "nesting" Javascript

Hi everyone.

I am trying to get this page to work using
<script src="url/test.php?webid=ADSasdfasdwerqy34"> //you get my point

in test.php I do something like this:

var test='hello world.';
document.write(test);
of course this works when I call the first page, it actually displays: hello
world.

But I now want something different. I want an alert coming up.

var test="<SCRIPT>
window.alert('hello world');
</SCRIPT";
document.write(test);

This does absolutely nothing. Can you help me please? I need this to work!

Thx

Michel


Dec 20 '05 #1
11 2660

Michel wrote:
Hi everyone.

I am trying to get this page to work using
<script src="url/test.php?webid=ADSasdfasdwerqy34"> //you get my point

in test.php I do something like this:

var test='hello world.';
document.write(test);
of course this works when I call the first page, it actually displays: hello
world.

But I now want something different. I want an alert coming up.

var test="<SCRIPT>
window.alert('hello world');
</SCRIPT";
document.write(test);


replace above with just:

window.alert("hello world");

Dec 20 '05 #2
"Michel" <no@spam.please> writes:
But I now want something different. I want an alert coming up.

var test="<SCRIPT>
window.alert('hello world');
</SCRIPT";
document.write(test);


I'll assume you have some reason for adding a new script element
instead of just its content to the current one.

I see two errors in the above.
First of all, a Javascript string literal cannot contain newlines.
Change it to:

var test="<SCRIPT>\n"+
" window.alert('hello world');\n"+
" </SCRIPT";

if you want exactly the same string as you seem to intend.

Second, you are missing an ">" after the "</SCRIPT".
A third potential error can occour if the above code is contained in a
script element in an HTML page, since the content of such ends at the
first occurence of "</". So, in total, change the line to:

var test="<SCRIPT>\n"+
" window.alert('hello world');\n"+
" <\/SCRIPT>";

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Dec 20 '05 #3

"web.dev" <we********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...

Michel wrote:
Hi everyone.

I am trying to get this page to work using
<script src="url/test.php?webid=ADSasdfasdwerqy34"> //you get my point

in test.php I do something like this:

var test='hello world.';
document.write(test);
of course this works when I call the first page, it actually displays: hello world.

But I now want something different. I want an alert coming up.

var test="<SCRIPT>
window.alert('hello world');
</SCRIPT";
document.write(test);


replace above with just:

window.alert("hello world");


O you so funny.

Anyone more serious?
Dec 20 '05 #4

Michel wrote:
"web.dev" <we********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...

Michel wrote:
Hi everyone.

I am trying to get this page to work using
<script src="url/test.php?webid=ADSasdfasdwerqy34"> //you get my point

in test.php I do something like this:

var test='hello world.';
document.write(test);
of course this works when I call the first page, it actually displays: hello world.

But I now want something different. I want an alert coming up.

var test="<SCRIPT>
window.alert('hello world');
</SCRIPT";
document.write(test);


replace above with just:

window.alert("hello world");


O you so funny.

Anyone more serious?


Sorry, perhaps I wasn't being clear enough. In your example, you are
invoking a php file indirectly via javascript, therefore the final
output of the php script needs to be a valid javascript. Therefore you
need to think of it as a dynamic .js file, bounded by the same
limitations as a regular .js file. Thus, to do what you are asking,
just do the following in your php file:

<?php
echo "window.alert('hello world');";
?>

Now of course since it isn't within a function block it will be
executed immediately.

Dec 20 '05 #5
web.dev wrote:
Michel wrote:
"web.dev" <we********@gmail.com> wrote [...]:
> Michel wrote:
> > I am trying to get this page to work using
> > <script src="url/test.php?webid=ADSasdfasdwerqy34"> //you get my
> > point
> >
> > in test.php I do something like this:
> > [...]
> > But I now want something different. I want an alert coming up.
> >
> > var test="<SCRIPT>
> > window.alert('hello world');
> > </SCRIPT";
> > document.write(test);
>
> replace above with just:
>
> window.alert("hello world");
O you so funny.
ISTM you (Michel) have no fully understood PHP.
Anyone more serious?


Sorry, perhaps I wasn't being clear enough. In your example, you are
invoking a php file indirectly via javascript, therefore the final
output of the php script needs to be a valid javascript. Therefore
you need to think of it as a dynamic .js file, bounded by the same
limitations as a regular .js file.


Correct.
Thus, to do what you are asking, just do the following in your php file:

<?php
echo "window.alert('hello world');";
?>


No, only content within `<?php ...?>' (or `<? ... ?>' with discouraged
short_open_tag=on) is parsed by PHP. Content outside of it is not parsed
by PHP, that is, it is served as-is: valid J(ava)Script code. Therefore
your first approach

window.alert('hello world');

(in the PHP resource, but outside of PHP-parsed content) suffices and is
more efficient than your second approach.

The OP should have tried before he complained.
PointedEars
Dec 20 '05 #6


PointedEars


Let's say I have my reasons to do it the way I am trying to get it
accomplished.

So if you can give any advice that get's me there, thanks
Otherwise, please do me a favor and type stupid remarks in another thread.

M

Dec 20 '05 #7
Michel wrote:


[my signature]

It is impolite to quote signatures without referring to them.
Let's say I have my reasons to do it the way I am trying to get it
accomplished.
You have not even explained what you expected the code to do and where!
So if you can give any advice that get's me there,
Maybe I even had one.
thanks Otherwise, please do me a favor and type stupid remarks in
another thread.


PLONK

(also for address munging -- another diregardment of Netiquette, and
violation of Internet Standards)
Dec 20 '05 #8

"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:51*****************@PointedEars.de...
Michel wrote:


[my signature]


It is impolite to quote signatures without referring to them.
Let's say I have my reasons to do it the way I am trying to get it
accomplished.


You have not even explained what you expected the code to do and where!
So if you can give any advice that get's me there,


Maybe I even had one.
thanks Otherwise, please do me a favor and type stupid remarks in
another thread.


PLONK

(also for address munging -- another diregardment of Netiquette, and
violation of Internet Standards)


PLonk to you too
Dec 20 '05 #9

var test="<SCRIPT>\n"+
" window.alert('hello world');\n"+
" <\/SCRIPT>";

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'


Hi Lasse,

Can you write me a snippet that works? I can't seem to get it to work
myself.

now:
in test.php I have

<HTML>
<BODY>
<SCRIPT SRC='in.php'></SCRIPT>
</BODY>
</HTML>

and in in.php I have

$test="<SCRIPT>\n"+window.alert('hello world');\n"+<\/SCRIPT>";
document.write($test);

Forget about the file extention. all other scripts are working fine like
this.
I want the alert to pop up then calling test.php

Thank you for your help!

Mich
Dec 20 '05 #10
Found the mistake. Thanks

"Michel" <no@spam.please> wrote in message
news:do**********@news.cistron.nl...

var test="<SCRIPT>\n"+
" window.alert('hello world');\n"+
" <\/SCRIPT>";

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Hi Lasse,

Can you write me a snippet that works? I can't seem to get it to work
myself.

now:
in test.php I have

<HTML>
<BODY>
<SCRIPT SRC='in.php'></SCRIPT>
</BODY>
</HTML>

and in in.php I have

$test="<SCRIPT>\n"+window.alert('hello world');\n"+<\/SCRIPT>";
document.write($test);

Forget about the file extention. all other scripts are working fine like
this.
I want the alert to pop up then calling test.php

Thank you for your help!

Mich

Dec 20 '05 #11
"Michel" <no@spam.please> writes:
Can you write me a snippet that works? I can't seem to get it to work
myself. .... and in in.php I have

$test="<SCRIPT>\n"+window.alert('hello world');\n"+<\/SCRIPT>";
document.write($test);
I assume this is what is sent by the PHP page (which is good, we don't
want no steenking PHP in this group :).

Is this valid Javascript?
If not, you should get some errors from your browser when you try it
(and if not, you *should* enable display of errors!)

Another way to check validity is with jslint:
<URL:http://www.crockford.com/jslint/index.html>
In this case, it balks at the "\" in "\n", because it occours outside
of a string. Probably because the string was not begun just
before "window.alert" (or before "<\/SCRIPT>")

Now, since you don't seem to know why you split the string or
why the newlines are there, you might as well not do it. Try just:

var $test = "<script>window.alert('hello world');<\/script>";

Actually, preferably:

var $test = "<script type='text/javascript'>window.alert('hello world');<\/script>";

(on one line, if your newsreader breaks the line, you should get new
one for reading groups where code is posted!)
I want the alert to pop up then calling test.php


What do you mean by "calling test.php"?

You don't call web pages, they are not functions. You request them,
and the response is evaluated on the client in some way.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Dec 20 '05 #12

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

Similar topics

3
by: Piet | last post by:
Hi, I am trying to generate an "hierarchical" layout based on wx.NoteBooks. That means, every page of a Notebook should be a NoteBookon its own. Here is a (short but complicated) piece of code...
0
by: Paul Lee | last post by:
Hi everyone, I'm trying to nest one XML document inside another, but the only method I can think of (not being familiar with Java and DOM!) would involve nested while loops. Basically, I have...
77
by: M.B | last post by:
Guys, Need some of your opinion on an oft beaten track We have an option of using "goto" in C language, but most testbooks (even K&R) advice against use of it. My personal experience was that...
12
by: M G Henry | last post by:
I have a tabbed form that contains 12 different "pages" and when I try and run the form I get the error message too many fields defined --- which I believe is the 255 field limit in the record...
25
by: bweaverusenet | last post by:
Hi. I am trying to get some javascript to work in IE6, from the address line. It works in Firefox and, I believe, IE7. It appears that with some number of nested structures, the code does not...
5
by: Maria Sudderman | last post by:
I have a prblem with the "onClick" command. onClick="insert('<a href="URI">', '</a>')"> but this is not correct! why? Maria
7
by: Nik Coughlin | last post by:
Further to my earlier post in alt.html, I ended up preparing these pages: http://nrkn.com/skinning/ They list different ways to "skin" an HTML element (ie add rounded corners, drop shadows,...
36
by: mdh | last post by:
May I ask the group this somewhat non-focused question....having now seen "continue" used in some of the solutions I have worked on. ( Ex 7-4 solution by Tondo and Gimpel comes to mind) Is there a...
17
by: henry | last post by:
Folks Here's a skeleton, generic HTML page, call it "index.php". You'll see a bit of php code in the middle: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.