2

I have looked at the multiple methods of inserting HTML with javascript, both with jQuery .prepend and .innerHTML and I keep getting the same output no matter what I do:

'); }

I am trying to insert HTML if the screen resolution if of a certain width. These are the two scripts I have tried:

<script type="text/javascript" charset="utf-8" >

if (screen.width >= 500)
{
$('#stemanimation_hype_container').prepend('<script type="text/javascript" charset="utf-8" src="STEM%20Animation_Resources/stemanimation_hype_generated_script.js?58608></script>');

}
</script>

<div id="stemanimation_hype_container" style="position:relative;overflow:hidden;width:900px;height:300px;">
</div>

The other:

<script type="text/javascript" charset="utf-8" >

if (screen.width >= 500)
{
document.getElementById("stemanimation_hype_container").innerHTML='<script type="text/javascript" charset="utf-8" src="STEM%20Animation_Resources/stemanimation_hype_generated_script.js?58608'></script>";

}

</script>
<div id="stemanimation_hype_container" style="position:relative;overflow:hidden;width:900px;height:300px;">
</div>

Any idea what may be going on? I am testing this on a page with only jquery and no other scripts to ensure no conflicts. Thank you for any help you can give me!

2
  • why are you adding charset="utf-8". I don't think you need that. Commented Sep 7, 2012 at 18:19
  • I'm using Hype to build animations, thats what it told me to do,but even without, it makes no difference Commented Sep 7, 2012 at 18:20

8 Answers 8

6

you have an error in the string defining the html to insert

'<script type="text/javascript" charset="utf-8" src="STEM%20Animation_Resources/stemanimation_hype_generated_script.js?58608'></script>";

should be:

'<script type="text/javascript" charset="utf-8" src="STEM%20Animation_Resources/stemanimation_hype_generated_script.js?58608"></script>';
Sign up to request clarification or add additional context in comments.

Comments

1

Your first block of code is missing a " after ?58608

Your second block of code has a ' where there should be a " after ?58608, and vice-versa for the end of the string.

Try

$('#stemanimation_hype_container').prepend('<script type="text/javascript" charset="utf-8" src="STEM%20Animation_Resources/stemanimation_hype_generated_script.js?58608"></script>');

1 Comment

dbaseman's example give me no brackets but no animation, jackwanders' give me brackets again.
1

Your original code is good, but this error:

..._script.js?58608"></script>');
                // ^ Forgot a "

Alternatively I may recommend using document.createElement and append it to the container though:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '..._script.js?58608';
document.getElementById('stemanimation_hype_container').appendChild(script);

Or, the jQuery way:

$('<script>',{
  'type' : 'text/javascript',
  'src' : '..._script.js?58608'
}).appendTo('#stemanimation_hype_container');

(Note I use ... in the code for brevity, not accuracy)

1 Comment

@BenjaminMorrison: Not sure what the issue is. Given this is hand-written script, the premise of attachment is the same: jsfiddle.net/BVJTK/1
0

If the code you posted is the actual code you're trying, you've got an unmatched double quote in the jq example, and a double closing a single in the other version.

5 Comments

What is your expected result? Since you're injecting a script, what do you hope to have happen?
I used a program called hype to create javascript/html/css3 animations. It works BEAUTIFULLY, but I just can't seem to get this to work conditionally with screen resolution. The site is responsive and I need to have different animations called/inserted depending on screen resolution.
If I upload it to my server would you guys mind taking a look yourself?
brmceramics.com/tiesrev/testing/STEM%20Animation.html
the one that works with the recommended code is here: brmceramics.com/tiesrev/testing/STEM%20Animationworking.html
0

Have you tried this:

$('#stemanimation_hype_container').html('<script type="text/javascript" charset="utf-8" src="STEM%20Animation_Resources/stemanimation_hype_generated_script.js?58608"></script>');

Comments

0

in both examples, i see inconsistent usage of quotes in your src attributes. you started with a double quote but ended with a single quote. try ending it with a double quote.

4 Comments

I fixed the quotes, no dice. If anyone wants to take a look it's at this address: brmceramics.com/tiesrev/testing/STEM%20Animation.html
Oh and FYI, I know jack squat about javascript.
I'm getting the following HTML:'); } Looks like an issue with the js file.
I didn't know that could be an issue. I thought that by calling the file by HTML, it wouldn't clash. I asked the Tumult forums but so far no bites
0

You need to put the script block at the end of your HTML. Right now, you are looking for $('#stemanimation_hype_container') before that div has been inserted into the DOM, and so it doesn't exist when the JS is run. That's why nothing happens.

Comments

0

My issue in getting this to work properly was actually a nasty conflict with Tumult Hype's script for CSS3 animation. The code examples, with the exception of my typo regarding the double quotes, is actually correct for most applications.

Comments

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.