0

In my code i'm trying to get a value that is set using a modelAttribute in a Spring controller and then pass it to Javascript function and set the value as a parameter in URL.

modelAttribute from the controller :

model.addAttribute("appName",appName); model.addAttribute("cloverAppType", cloverAppType);

the javascript in the view :

 <a data-toggle="tab" onclick="loadOrders()">Orders</a>

  function loadOrders() {
    var message = "${appName}".replace(/'/g, "%27");
    var x = encodeURIComponent(message);
    window.location = '/admin/business/${businessId}/order?cloverAppType=${cloverAppType}&appName='+x;
   }

The issue is when a ' is included in the appName it breaks the javascript and give the Uncaught SyntaxError: Unexpected identifier and Uncaught ReferenceError: loadOrders is not defined at HTMLAnchorElement.onclick errors on the browser console.

But if I hardcode the value Poppo's Taqueria - Outpost its being replaced and encoded.

How can I avoid this and pass the appName value with the ' character as a aparameter of the URL?

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
GeekySelene
  • 847
  • 3
  • 13
  • 31

1 Answers1

0

change

var message = "${appName}".replace(/'/g, "%27");

to

var message = `${appName}`.replace(/'/g, "%27");
Alex
  • 11
  • 3