2

I need the following code in a variable rendered as html and not as a string:

<div style="display:table-cell; vertical-align:middle">
    <center>
        <h1>TO THE TIME</h1>
        <script type="application/javascript">
           var myCountdown1 = new Countdown({
                                //year      : 2015,
                                month   : 11,    
                                day     : 11,     
                                hour    : 00,    
                                ampm    : "am",  
                                minute  : 00,         
                                second  : 00,       
                                width:290, 
                                height:60,  
                                rangeHi:"month",
                                rangeLo:"seconds",
                                style:"flip"
                            });

        </script>
    </center>
</div>

I tried storing it as

htmlcontent[idgrab] = '<div style="display:table-cell; vertical-align:middle">'+
'<center>'+
'<h1>TO THE TIME</h1>'+
'<script type="application/javascript">'+
'var myCountdown1 = new Countdown({'+
                                'month  : 11,'+    
                                'day    : 11,'+     
                                'hour   : 00,'+    
                                'ampm   : "am",'+    
                                'minute : 00,'+         
                                'second : 00,'+       
                                'width:290,'+ 
                                'height:60,'+  
                                'rangeHi:"month",'+
                                'rangeLo:"seconds",'+
                                'style:"flip"'+
                                '});'+

'</script>'+
'</center>'+
'</div>';

But that didn't work, basically I have code that pulls dynamically created html which I managed to grab using .html however this one I've only managed to show up as either a string using ""; or break the site entirely.

Edit:

The normal renderer pulls code with this:

htmlcontent[idgrab] = $('#'+idgrab).html();

which works fine for basic html that does not include javascript but for this div in particular, it seems to need to be manually slotted into the array which im having trouble with because the div needs to be stored WITHOUT executing the javascript code (it needs to be executed after the array is invoked).

2
  • "I have code that pulls dynamically created html", which code? How are you rendering your html. Chances are the issue is there (it's probably escaping < to &lt; and so on). Commented Jun 9, 2015 at 1:15
  • I recommend making it clear what libraries you're using. It appears that you're using the countdown timer from this website. gieson.com/Library/projects/utilities/countdown Commented Jun 9, 2015 at 3:02

3 Answers 3

2

Try utilizing $.parseHTML

var htmlcontent = {};

htmlcontent["idgrab"] = '<div style=display:table-cell; vertical-align:middle>'
+ '<center>'
+ '<h1>TO THE TIME</h1>'
+ '<script type="application/javascript">'
+ 'var myCountdown1 = new Countdown({'
+ 'month'+  ':'+ '11,'
+ 'day'+ ':'+ '11,'
+ 'hour'+   ':'+ '00,'
+ 'ampm' +  ':' + '"am",'
+ 'minute' +':'+ '00,'
+ 'second' + ':' + '00,'
+ 'width' +':' + '290,'
+ 'height' +':' + '60,'
+ 'rangeHi' + ':'+ '"month",'
+ 'rangeLo' +':' + '"seconds",'
+ 'style' +':' + '"flip"'
+ '});'
+ '</scr'+'ipt>'
+ '</center>'
+ '</div>';

var html = $.parseHTML(htmlcontent["idgrab"], document, true);
console.log(htmlcontent["idgrab"], html);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

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

1 Comment

I mean yours worked but when the div is rendered, it literally replaces the page with just the timer, same with the other proposed solutions (except I couldn't get parsehtml to work).
0

Normally you want to keep JavaScript out of the HTML entirely and just bind to the relevant DOM events, e.g. $(document).ready(function(){}) or $(window).load(function(){}). Most UI frameworks let you hook into several events, including when the element is first rendered to the DOM.

If you need to output HTML for use in dynamic widgets, you can try escaping them before rendering them in the DOM, e.g. htmlspecialchars for PHP or escape-html's escape method for Node.js. If you took that route, best practice is probably to put that into a data-* attribute like jQuery Cycle 2 would have you do.

7 Comments

It absolutely must contain javascript just because I need the javascripts function to execute at a certain location as it will create a timer wherever it is executed
So you need to control the context of the execution, which can be done using <function-name>.apply?
@GinzoMilani Click the blue square to see it.
@GinzoMilani ... and also definitive proof that you don't need the JS to be at exactly that location in the HTML.
I used yours mixed with guest271314s reply but the timer literally replaces the whole page
|
0

If you'd like to parse html string into a DOM element (tree) without jquery you an do this

var parseHTML = function(str) {
  var tmp = document.implementation.createHTMLDocument();
  tmp.body.innerHTML = str;
  return tmp.body.children;
};

parseHTML(htmlString);

Or simply use $.parseHTML(htmlString); if you have jquery in your site

Working jsfiddle

2 Comments

I have tried to use parseHTML however it shows up as it doesn't exist I am running a cloud version of jquery from here: code.jquery.com/jquery-2.0.3.min.js
@GinzoMilani I've added working fiddle to the answer using your jquery link

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.