1

I am trying to create a dynamic hyperlink using JavaScript. I am using "&" to pass multiple arguments, but ii am getting a parsing error.

JavaScript:

<script>
    function visitPage() {
        var phn = document.getElementById("bid").value;
        var totalprice = spans[3].innerHTML;
        window.location.href = 'https://www.instamojo.com/stayuncle/payments-for-stayuncle?data_amount='
            + totalprice + '&' + 'data_Field_68092=' + phn;
    }
</script>

Error:

The entity name must immediately follow the '&' in the entity reference.

I tried to fix it by passing '&AMP', which resulted in the link looking like this: https://www.instamojo.com/stayuncle/payments-for-stayuncle/?data_amount=1374.75&AMPdata_Field_68092=Hotel%20Delhi%20Airport%20Link

'&AMP' is still in link, instead of getting converted to an ampersand itself.

I have also removed the semicolon (;) after every &AMP, because it didn't let me post actual problem.

Philipp Maurer
  • 2,480
  • 6
  • 18
  • 25
nand
  • 517
  • 2
  • 13
  • 29
  • What is in `phn` and `totalprice`? – jfriend00 Aug 15 '15 at 19:53
  • @Jens I was using & only . I was not able to type and to make it distinguish so I make it capital in my question – nand Aug 15 '15 at 19:56
  • @jfriend00 totalprice containing double like 112.00 and phn is integer in nature – nand Aug 15 '15 at 19:58
  • @vinayakj I tried with encodeURIComponent() .its not changing domain .Its mixing two domain .. one is local host and another is on which I am directing it . – nand Aug 15 '15 at 20:16
  • Please do not tag a Javascript question with [tag:java]. They are totally different languages. – RealSkeptic Aug 15 '15 at 20:46

4 Answers4

2

var a = 1374.75;
var b = 'Hotel Delhi Airport Link';
var url = 'https://www.instamojo.com/stayuncle/payments-for-stayuncle'
var link = decodeURIComponent(url+'?data_amount='+a+'&data_Field_68092='+b)

alert("Redirecting to"+ link)
location.href = link

Alsso need to wrap the code in <![CDATA[ code //]]> as using the Thymeleaf.

vinayakj
  • 5,591
  • 3
  • 28
  • 48
  • I have used same code that you have written but getting this error **The reference to entity "data_Field_68092" must end with the ';' delimiter.** . I am using thymeleaf at UI front. Please let me know how can I resolve this – nand Aug 15 '15 at 21:00
  • you need to create small fiddle, as above code runs correctly – vinayakj Aug 15 '15 at 21:05
  • you need to provide exact code snippet that is causing problem, as above code in javascript works correctly – vinayakj Aug 16 '15 at 16:59
  • after adding // <![CDATA[ your code //]]> .YOur code was working for me . As am using thymeleaf and it based on XML that's why I was getting parsing error and it resolved after addition of // <![CDATA[. I also added code snap below – nand Aug 16 '15 at 20:03
1

As I am using Thymeleaf as a front end and Thymeleaf using XML underlying.so in such scenario CDATA section is required. Here is code snap that work for me. for more detail refer this section When is a CDATA section necessary within a script tag?

 // <![CDATA[

    var a = 1374.75;
var b = 'Hotel Delhi Airport Link';

var link = decodeURIComponent('https://www.instamojo.com/stayuncle/payments-for-stayuncle?data_amount='+a+'&data_Field_68092='+b)

alert("Redirecting to"+ link)
location.href = link

 //]]>
After adding // work for me as Thymeleaf under lying using XML so To implement & and other special symbol need to add "
Community
  • 1
  • 1
nand
  • 517
  • 2
  • 13
  • 29
  • ohk.. didnt know about the Thymeleaf way.. was considering as just the code snippet from javascript file.. – vinayakj Aug 16 '15 at 20:23
0

var totalprice = 1374.75;
var name = 'Hotel Delhi Airport Link';
var email='n@gmail.com';
var phn='1111111111'

var link = decodeURIComponent('http://www.example.com?data_amount='+totalprice+'&amp;'+'data_name='+name+'&amp;'+'data_email='+email+'&amp;'+'data_phn='+phn)

alert("Redirecting to"+ link)
location.href = link
nand
  • 517
  • 2
  • 13
  • 29
  • @vinayakj I have posted code snap by some addition into your code . here I have used '&' instead of & because Thymeleaf show this parsing error on use of (&) **HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Exception parsing document:The reference to entity "data_Field_68092" must end with the ';' delimiter.** but '&' is not converting to & – nand Aug 16 '15 at 05:34
0

var x = 1374.75;
var y = 'Tesxt Data';

var link = decodeURIComponent('URL='+x+'With Field='+y)

alert("Redirecting to"+ link)
location.href = link
Anand Dwivedi
  • 1,452
  • 13
  • 23
  • How can I pass two more arguments? let say y & z ? – nand Aug 16 '15 at 09:05
  • I tried but there is no separation between elements that we are passing .(+) treated everything as one .Look at the out come of our code snippet .It doesn't differentiating value of x from field "With Filed" .all treated as one parameter – nand Aug 16 '15 at 11:02