Solved

JavaScript syntax question (Urgent please)

Posted on 2015-02-17
1
87 Views
Last Modified: 2015-03-13
I am using the following JavaScript code in a .html file for my Jetty web application:

<script id="user-template" type="text/x-handlebars-template">
    <img src="assets/images/admin/redminus.png" class="user-delete-img">
    <div class="user_name admin-users-span-1">{{username}}</div>
    <div class="user_email admin-users-span-2">{{username}}</div>
</script>

The above script works. It obtains the "username" variable as expected. I am creating another .html file where I am using the same "username" variable but I am experiencing problem with syntax and it is not working. The following are the .html file code. I have written three versions of it. Could you please let me know which one has the correct syntax? The first one did not work. If none has the correct syntax, could you please help me correct it?

1.
<script id="footer-template" type="text/x-handlebars-template">
        <img src="http://mypage.com/t.png?t=map&p={{username}}"/>
</script>

2.
<script id="footer-template" type="text/x-handlebars-template">
        <img src="http://mypage.com/t.png?t=map&p="/>{{username}}
</script>

3.
<script id="footer-template" type="text/x-handlebars-template">
        <img src="http://mypage.com/t.png?t=map&p="{{username}}/>
</script>
0
Comment
Question by:toooki
1 Comment
 
LVL 16

Accepted Solution

by:
Lucas Bishop earned 500 total points
ID: 40615939
What did not work with the first one? How did it render in your page source? Does t.png exist in the root directory?

I see version 1 as being correct. Here is an example of how each would render if username was johndough:

Version 1 would include johndough as a parameter on the image path with proper html syntax:
        <img src="http://mypage.com/t.png?t=map&p=johndough" />

Open in new window


Version 2 would render the image with an empty parameter for "p=" and the username "johndough" rendered in plain text next to the image.
<img src="http://mypage.com/t.png?t=map&p=" />johndough

Open in new window


Version 3 the image path would end with an empty "p=" and the username would likely cause the image to appear broken since it is not defined as being part of valid html code.
<img src="http://mypage.com/t.png?t=map&p="johndough />

Open in new window


If you create an image tag manually on the page, using a real username in the parameter string, does it render properly? For example:

        <img src="http://mypage.com/t.png?t=map&p=replacewithrealusername" />

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now