Login | Register | FAQ
Anonymous

Include Code in another Script Tag

+ Post a reply

4 posts Page 1 of 1


Include Code in another Script Tag

by hasib » Fri Feb 03, 2012 8:23 pm

Why this JS code dont working? I want to include "show();" in the second script tag. But it doesn't working. But why?
The code:
<html>
<head>
<title>te st</title>
</head>
<body>
<p>it is a paragraph</p>
<script type="text/javascript">
function show()
{
document.write(new Date());
}
var d=document.getE lementsByTagName("script");
d[1].text="show();";
</script>
<p>its another para</p>
<script type="text/javascript"></ script>
</body>
</html>
User avatar

hasib

  • Posts: 7
  • Joined: Tue Jan 04, 2011 3:52 am
  • Location: Khulna, Bangladesh

Re: Include Code in another Script Tag

by mariaantonietta » Sat Feb 04, 2012 2:53 pm

Hi Hasib,

It's an odd thing what you're trying to do. However, here's what I've come up with. First, you need to replace .text() with innerHTML. Secondly, you need to change the order in which you put the script tags with respect to each other: the tag inside which you want to place show(); has to come before the script tag that contains the code to do that. Thirdly, you then need to replace d[1] with d[0] (because the sequential order of the script tags is now reversed. Finally, the result is only visible if you have developer's tools on your browser that allow you to view generated source, otherwise nothing seems to happen on the page. If you intended to call the show() function in this way, this is not possible. Here's the relevant chunk of the code:

Code: Select all
  <p>it is a paragraph</p>

<p>its another para</p>
<script type="text/javascript">
</script>

<script type="text/javascript">
function show()
{
document.write(new Date());
}
var d=document.getElementsByTagName("script");
d[0].innerHTML = "show();";

</script>


hasib wrote:Why this JS code dont working? I want to include "show();" in the second script tag. But it doesn't working. But why?
The code:
<html>
<head>
<title>te st</title>
</head>
<body>
<p>it is a paragraph</p>
<script type="text/javascript">
function show()
{
document.write(new Date());
}
var d=document.getE lementsByTagName("script");
d[1].text="show();";
</script>
<p>its another para</p>
<script type="text/javascript"></ script>
</body>
</html>
User avatar

mariaantonietta

  • Posts: 64
  • Joined: Mon Oct 31, 2011 7:23 pm

Re: Include Code in another Script Tag

by hasib » Sat Feb 04, 2012 6:05 pm

Thanks Mariaantonietta!
That means JS cant hook the content which is placed after the code? Am i right? Another question, then how JS hook up the contents in the body while JS is placed in the head tag? In this case the content is placed after the JS code too :/ what's the diffrent of this two case?
And another question... Unlike other programming language(like PHP,C,C ,python) we can call function which yet not defined, but after the call, we can define the function! And it still work! Whats the matter?? I'm feeling frightened with it!!
Code: Select all
<script type="text/javascript">
alert(add(5,3));
function add(a,b)
{
return a+b;
}
</script>


why it works??
User avatar

hasib

  • Posts: 7
  • Joined: Tue Jan 04, 2011 3:52 am
  • Location: Khulna, Bangladesh

Re: Include Code in another Script Tag

by mariaantonietta » Sat Feb 04, 2012 6:42 pm

Hi Hasib,

JavaScript can get a hook on whatever finds already loaded in the browser after it itself has finished loading, except for variables that have local scope, that is, that live inside a function. It uses the DOM (Document Object Model), that is, a sort of map that it builds of the elements in the document.

hasib wrote:Thanks Mariaantonietta!
That means JS cant hook the content which is placed after the code? Am i right? Another question, then how JS hook up the contents in the body while JS is placed in the head tag? In this case the content is placed after the JS code too :/ what's the diffrent of this two case?
And another question... Unlike other programming language(like PHP,C,C ,python) we can call function which yet not defined, but after the call, we can define the function! And it still work! Whats the matter?? I'm feeling frightened with it!!
Code: Select all
<script type="text/javascript">
alert(add(5,3));
function add(a,b)
{
return a+b;
}
</script>


why it works??
User avatar

mariaantonietta

  • Posts: 64
  • Joined: Mon Oct 31, 2011 7:23 pm


+ Post a reply

Page 1 of 1