8

Support that I have this hidden field:

<input type='hidden' id='handler' name='handler' value='5534882394' />

And imagine that I fetch an HTML fragment from server via jQuery AJAX.

<div id='result'>
    <span></span>
</div>

However, instead of writing something like this:

$(function(){
   $('#result span').text($('#handler').val());
});

I'd like to use something like:

<div id='result'>
    <span>javascript:$('#handler').val();</span>
</div>

However, I don't get the intended result. Can I use this approach with JavaScript?

Update: Everybody please, I know about $(document).ready();. So, don't provide better ways. I just wanted to know if it's possible to have inline JavaScript, or not, and if yeah, how?

2
  • 3
    If you're using Jquery, please don't use inline. You have better ways. Commented Aug 13, 2011 at 13:10
  • Why do you want to do it that way? What purpose does it serve? Commented Aug 13, 2011 at 13:11

6 Answers 6

14

No, you can't use that approach with Javascript. There is no such thing as inline Javascript.

What you see in a link like <a href="javascript:alert(1)"> is the javascript: pseudo-protocol, similar in use to the http: protocol. When you point the browser to such an URL, the browser runs the script.

If you want to run a script in the page, you need a script tag.

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer @Guffa, you really deserve your high score buddy. :)
6

You could do -

<input type="text" id="handler" value="yes"/>
<div id='result'>
    <span>
       <script>document.write($('#handler').val());</script>
    </span>
</div>

and the page would run the Javascript when it hit the script tag. You'd have to make sure that '#handler' element was already loaded though or the script would fail.

In general it would be better to keep script tags such as this away from your mark-up.

Working demo - http://jsfiddle.net/ipr101/h6zXs/

2 Comments

That doesn't do what the OP wants.
That statement returns a string value, but that doesn't mean that that string will be put inside the SPAN element. SCRIPT elements are not replaced with the return value of their statements.
6

Although the <script> tag works and would let you do what you are trying to do, I would really suggest rethinking your design there. This is not directly what you would call "unobtrusive Javascript".

Why do you prefer to mix HTML and JS? If it's for curiosity - OK, but if this is intended to turn into production code then you might want to separate the two as much as you can. You will be rewarded with a much better maintainability in the end.

Comments

3

Have you forgotten about the <script> tag?

<div id="result"></div>
<script>$('#result').text($('#handler').val();</script>

3 Comments

thanks, but I said inline JavaScript. A JavaScript you use in href of an a element: <a href='javascript:void(0);'></a>. This doesn't require <script> tag.
so what do you want to achieve?
Script tags are inline JavaScript. Maybe you're looking for something more like api.jquery.com/jQuery.tmpl.
2

A few years back, I had that exact problem: I was using AJAX to retrieve HTML and that HTML had embedded Javascript. I was never able to find a better solution than parsing the HTML manually to dig out the Javascript tags and calling eval on their contents. Hackish and unreliable, but it was the best I could find.

Comments

-1

This answer is for educational purposes only. Don't actually do this.

This code will do the trick:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<input type='hidden' id='handler' name='handler' value='5534882394' />
<div id='result'>
    <img src='invalid' onerror="$(this).replaceWith($('<span>').text($('#handler').val()))"/>
</div>

In ES6:

<input type='hidden' id='handler' name='handler' value='5534882394' />
<div id='result'>
    <img src='invalid' onerror="
        let newElem = document.createElement('span');
        newElem.textContent = document.getElementById('handler').value;
        this.replaceWith(newElem);
    "/>
</div>

The way this works is that as soon as the img element is loaded, the browser tries to fetch src. Since src is an invalid value, onerror is immediately triggered which can do whatever you want — including replacing the entire element.

As previously said, a much more sensible solution is to just use a script tag.

<input type='hidden' id='handler' name='handler' value='5534882394' />
<div id='result'>
    <span></span>
    <script>
        document.querySelector('#result > span').textContent = document.getElementById('handler').value;
    </script>
</div>

3 Comments

Thank you for providing these examples! Note that the third one however does not trigger "onError" only, but on every page load. You might need to change that snippet to only run in the onError handler of the input. Something like document.getElementById('handler').onerror = ()=>{ document.querySelector('#result > span').textContent = document.getElementById('handler').value;}
@GlabbichRulz Sorry, I don't understand this comment. How can the <input> element ever trigger an error?
my bad, i was thinking of the onError event of the img, not the input.

Your Answer

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.