0

I need to put a link out from a corporate site to a surveymonkey survey. Our site uses a proprietary CMS limiting me from adding any proper function or third party plugin.

After evaluating options like those exposed in this other question, I believe I call the correct javascript function but everytime I open my CMS, the link duplicates itself... leading me to think I've done something inapropriate.

Things look acceptable on the JSFiddle demo I put together for this question but I'm hoping you'd have a more elegant solution in mind so I could try options !

<script type="text/javascript">
document.write("<a href='https://www.surveymonkey.com/r/[SURVEYID]?url=" + window.location.pathname + " target='_blank'>Test - survey</a>");
</script>
Greg
  • 354
  • 1
  • 5
  • 11
  • [encodeURIComponent()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) may be useful so the url will not read the path as part of the route, the only problem is it would need to be converted back assuming that survey monkey doesn't – Patrick Barr May 24 '17 at 14:18
  • Your jsfiddle demo is incorrectly including the target property name in the url. It looks like you're missing a single quote: `document.write("Test - survey");`. I don't think this will solve your current issue, however. – Joseph Marikle May 24 '17 at 14:21

2 Answers2

2

Try this - it will probably not do what you want in one go, but it will hopefully isolate your problem so that you can better pinpoint what's going wrong:

HTML:

<div id="link"></div>

Javascript:

var SURVEYID = 3
var a = document.createElement("a");

a.innerHTML = "Test - survey";
a.href = "www.surveymonkey.com/r/" 
  + SURVEYID 
  + "?url=" 
  + window.location.pathname 
  + "&target=_blank"

document.getElementById("link").appendChild(a)

I'm afraid there can be multiple things going wrong, but I hope you can now distinguish between the various parts that your URL is built up from.

Sventies
  • 2,314
  • 1
  • 28
  • 44
1

This is mostly just a theory because I don't know your CMS or how it works, but I'm assuming that the CMS is inlining the javascript, executing it, and retaining that as its content along with the script. This would create that duplication. The original intent of using document.write I would assume was to completely replace the content; but if it's inlined, it only appends. An external script would completely replace. See below:

All of this text is retained.
<script type="text/javascript">
document.write("<a href='https://www.surveymonkey.com/r/[SURVEYID]?url=" + window.location.pathname + "' target='_blank'>Test - survey</a>");
</script>

In this demo, we use document.body.innerHTML instead. This will replace the content completely.

None of this text will be retained.
<script type="text/javascript">
document.body.innerHTML = "<a href='https://www.surveymonkey.com/r/[SURVEYID]?url=" + window.location.pathname + "' target='_blank'>Test - survey</a>";
</script>

If true, complete replacement of the body content is your goal, innerHTML is probably what you need.

Edit + Warning:

This may make the page inaccessible from the CMS depending on how it's built. It may make editing the page impossible.

Edit

Here's a better solution. Just set the href of the anchor by first getting it by the ID. This was based off of Sven ten Haaf's Answer.

<a href="#" id="__smlink" target='_blank'>Test - survey</a>
<script>
 document.getElementById('__smlink').href = "https://www.surveymonkey.com/r/[SURVEYID]?url=" + window.location.pathname;
</script>
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • As you noted, CMS is no longer accessible ! It did look great for a minute though. I'll try restoring a previous version of the page. – Greg May 24 '17 at 14:41
  • @Greg ... oops. Sorry. So then that means the CMS is executing the script tag in the editor. Not ideal, but it is what it is. Can't you just add the anchor tag the same way you're adding the script? – Joseph Marikle May 24 '17 at 14:43
  • nvm... it's suppose to be dynamic to the existing URL. Try Sven's answer. That will be a much better solution than `document.write` or `document.body.innerHTML` – Joseph Marikle May 24 '17 at 14:44
  • 1
    @Greg I've added a third option. That should be a bit more robust. – Joseph Marikle May 24 '17 at 14:49