2

I want pass a parameter of javascript functiona which is a string. This javascript function is a hintbox on mousehover..

and the string i am using is like this:

Hemmed Finish: Every side/edge (1/2" to 2") of the banner are folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.

Stitched Finish: Every side/edge (1" to 2") of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner.

Now in the hintbox on mousehover the above given text has to be display as it is displayed above along with the paragraph break.. But when i pass the above as parameter in that function along with appending some backslashes to recognise some punctuation, iots till gives me javascript error of unterminated string...

I am doing this:

onMouseover="showhint('Hemmed Finish\: Every side/edge \(1/2\'\' to 2\'\'\) of the banner are folded and glued \(special vinyl solution\) or heat pressed. This is the most common and best finish option.Stitched Finish\: Every side/edge \(1\'\' to 2\'\'\) of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner', this, event, '250px')"

pls could u help me in rectifying the issue...

7
  • 1
    can you post the code? if you are escaping the quotes, there must be something else wrong in the function. Commented May 28, 2010 at 12:40
  • I have given the function call i m doing in my code I know only this has to be rectified.. pls see and help me out Commented May 28, 2010 at 12:42
  • This seems to work fine, using alert in place of showhint. What happens in showhint? Commented May 28, 2010 at 12:44
  • its just a function to display the tooltip on mouseover on the image Commented May 28, 2010 at 12:45
  • 1
    Exactly what does showhint() do? Commented May 28, 2010 at 12:58

5 Answers 5

4

Ok, I have the solution:

<element onMouseover="showhint('&lt;p&gt;Hemmed Finish: Every side/edge (1/2&quot; to 2&quot;) of the banner is folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.&lt;/p&gt;&lt;p&gt;Stitched Finish: Every side/edge (1&quot; to 2&quot;) of the banner is folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner&lt;/p&gt;', this, event, '250px')" />

You have to escape the quotes XML-style, using &quot;.

See a working example here.

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

7 Comments

HTML style. It just happens to be the same as XML style.
I thought the relationship was the other way around.
I checked ur code and its not giving me two different paragraphs as i am getting using a single <p> as i did
first of all look out the question properly I was just asking for the solution to display the paragraph change, not the other hocuspocus....
@eric now pls my updated answer... i know using tags is still unvalid as u r saying but this time i have closed the tag as per requirement of validation and removed the unnecessary backslashes.
|
1

I see two potential problems here.

  1. JavaScript, like many other programming languages, uses the " character to mark the beginning and the end of a string. Your hintbox text contains a ", so when JavaScript encounters it, it thinks that your string has ended. To fix the problem, you should refer to each " in your string as \". This tells JavaScript that you don't intend to end the string, but rather you plan to put a " inside your string.

  2. Similarly, JavaScript does not allow you to place a "paragraph break" directly inside a string. Instead, you must type \n each time you want a carriage return inside your string.

So, here is how you should define your hintbox string:

hintbox = "Hemmed Finish: Every side/edge (1/2\" to 2\") of the banner are folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.\n\nStitched Finish: Every side/edge (1\" to 2\") of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner.";

This is typically called "escaping strings", and a quick Google should give you much more information on the toppic.

Happy programming!

Comments

1

Check this :

onMouseover="showhint('Hemmed Finish: Every side/edge (1/2\'\' to 2\'\') of the banner are folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.Stitched Finish: Every side/edge (1\'\' to 2\'\') of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner', this, event, '250px')"

you do not need to escape : brackets like (

<script type="text/javascript">
<!--//
function alert2()
{
alert ("Don't forget the curly "+
"brackets - \nThey are essential!");
}
//-->
</script>

1 Comment

Pranay I need the tooltip to display the string in 2 different paragraphs i have given in question..
1

Whilst the example works for me, it's pretty ugly. Avoid inline event handlers and the crazy escaping problems that come with them. Put the data elsewhere, such as in a JS variable or an attribute value where appropriate. For the case of a tooltip, the title attribute is best:

<span title="Hemmed Finish: Every side/edge (½″ to 2″) of the banner are folded and glued (special vinyl solution) or heat pressed. This is the most common and best finish option.&#10;&#10;Stitched Finish: Every side/edge (1″ to 2″) of the banner are folded in the back and stitched/sewed with white thread. This is not a common option as thread can be seen on the banner.">
    finish options...
</span>

Whilst that works as a tooltip in itself, you can use progressive enhancement to replace that native-HTML tooltip with a scripted one if you want eg. the more flexible styling that comes with it:

var spans= document.getElementsByTagName('span');
for (var i= spans.length; i-->0;)
    if (spans[i].title)
        replaceTitle(spans[i]);

function replaceTitle(element) {
    var title= element.title;
    element.title= '';
    element.onmouseover= function(event) {
        showhint(title, this, event||window.event, '250px');
    };
    element.onmouseout= function(event) {
        hidehint(...);
    };
}

Comments

-1

You have to escape " characters. So instead of " use \" (backslash). You could use the new line character to create the paragraph \n or if your tooltip is capable of displaying html use

or
for the paragrahps.

I suggest you also check out jquery tooltip Tooltip.

1 Comment

The " terminates the attribute value and `` is not an escape character in HTML.

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.