0

Is it possible to do somthing like this ?

<script type='text/javascript' src='xxx.js'>
 alert('Can I say hello in this area?');
</script>

Thanks.

asked Mar 30, 2011 at 3:11
3
  • why would you want to do that? Commented Mar 30, 2011 at 3:13
  • 2
    What happened when you tried it? Commented Mar 30, 2011 at 3:14
  • Purpose is to reduce script tag. Commented Mar 30, 2011 at 7:12

4 Answers 4

1

No, but it is possible to do this:


<script type='text/javascript' src='xxx.js'>
 alert('Can I say hello in this area?');
</script>
<script type='text/javascript'>
 eval([].pop.call(document.getElementsByTagName('script')).innerHTML);
</script>

It eval's the .innerHTML of the last script tag that was closed.

It is also possible to include the eval code in your external file, the last script tag should evaluate to the script tag that initialized the file download.

answered Mar 30, 2011 at 3:46
Sign up to request clarification or add additional context in comments.

Comments

1

Nope.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js">
 var doesItWork = "#myElement";
</script>
<script type="text/javascript">
 $(document).ready(function() {
 alert("did it work?");
 $(doesItWork).text("yes");
 });
</script>
<div id="myElement">no</div>
answered Mar 30, 2011 at 3:19

Comments

0

No, you need to use a separate script, and do not use a self-closing tag, I've wasted many hours figuring out a problem because I used a self-closing script tag.

Use:

<script type="text/javascript" src="xxx.js"></script>

NOT

<script type="text/javascript" src="xxx.js" />
answered Mar 30, 2011 at 3:14

Comments

0

Doesn't work for me in Chrome, Firefox, or Safari so I'm going to say no it's not possible.

answered Mar 30, 2011 at 3:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.