0

This is what I am doing: I am building a fun in house API Voting System. I am using a client side snippet insert onto page

Like this:

<script src="domain.com/api/scripts/main.js"></script>
<div id="content-wrap" id="ac1e435e-c564-48f8-9f45-338616e7a789"></div>

Now in my main .JS I do all ajax request and modify the #content-wrap with creating new elements and inserting additional JS required to run Voting System.

However big issue I am experiencing is when I write JavaScript that I need to insert into #content-wrap I am currently writing it like this:

script.innerHTML = "$(someting).on('click', funciton(){"
+ "$.ajax({type: 'post',"
+ " url: '" + base + "/api/request', data: $('form').serialize(), "
+ "success: function(response){";

As you can see that can cause lot of issues as I build on it. What is better way to accomplish this or is there a way i can just write my script / code and do something like this.

script.innerHTML = ConvertToString(script.js) OR ConvertToString(function X);

ConvertToString is just an expression I am using to explain what I would like to do instead of what I am doing.

Thank you, I am open to any suggestions. I also must do this in plain JavaScript or with jQuery library so any suggestions to use VueJs, AngularJS or React will be considered as future references.

Thank you again

Additional explanation:

I would like to insert into my script element JavaScript snippet. But my snippet is about 30 lines long currently and might get bigger with time so it is very difficult to code with all the + " code " on every line that I write so that it can be inserted with innerHTML into element and executed on Client end.

So I would instead like to do something like this

element.innerHTML = mysnippetcode // but with out using + "" on each line like shown above

OR

element.append(snippet)

I hope this makes it little more clear

Solution that worked for me was using back ticks to wrap my sinppet and insert it into innerHTML of the element..

IamCavic
  • 368
  • 1
  • 15
  • 1
    There's really no need to write javascript into innerHTML? Also, typos – adeneo Sep 15 '17 at 15:49
  • Possible duplicate of [How to get html to print return value of javascript function?](https://stackoverflow.com/questions/6957917/how-to-get-html-to-print-return-value-of-javascript-function) – Tom O. Sep 15 '17 at 16:00
  • Even with your _Additional explanation_ you're describing an [X-Y problem](https://meta.stackexchange.com/a/66378/138482). As @Taurus asked, _"what do you want to achieve really ?"_ You answer with "I would like to insert into my script element JavaScript snippet" but that is your proposed solution to what you are _actually_ trying to achieve. Maybe injecting JS as a string is not the way to go about doing what you're _really_ trying to accomplish. – Stephen P Sep 15 '17 at 16:54

1 Answers1

1

Just use the function's name without the () to convert it to a string:

function foo() {
var a = 10;
var b = 20;
var c = a + b;
return c;
}


document.write(foo);

The document.write will result in this string:

function foo() { var a = 10; var b = 20; var c = a + b; return c; }

If you only want the function's body, then you could just normally remove the first and last characters of the string.

I am not entirely sure this is what you wanted, if not, please make yourself more clear.

Alternatively, you could do an eval([insert function code here]) and there would be no need to add the code to the innterHTML of the script, read up on that function if you haven't heard of it.

Or if you want to create a function from a string, you can use new Function([name] ,[function body string]) if you need arguments you have to sandwich them between the 2 parameters.

But my snippet is about 30 lines long currently and might get bigger with time > so it is very difficult to code with all the + " code " on every line that I write

You can use template literals if you want multi-line strings in Javascript, you simply have to replace your quotes with backticks.

See this MDN page if you are interested, or even this StackOverflow answer.

doubleOrt
  • 2,407
  • 1
  • 14
  • 34
  • Thank you I have tried both of your suggestions but they didn't provide the result I wanted. – IamCavic Sep 15 '17 at 16:39
  • @IamCavic what do you want to achieve really ? Are you trying to generate some HTML according to the AJAX data you get ? – doubleOrt Sep 15 '17 at 16:40
  • I just updated my question with more info.. What I am doing is creating and populating wrapper on FIRST call to server... Now at this time I want to add additional JAVASCRIPT code that when user interacts with the content it would do things.. I got all that working however my additional JavaScript I am adding via .innerHTML = javascriptcode is all written as a string and it is very difficult to keep track of what I am doing.. So I would like to insert that sinppet into dom element with out having to write it as a string – IamCavic Sep 15 '17 at 16:46
  • @IamCavic So you have 2 questions: 1. insert something into a ` – doubleOrt Sep 15 '17 at 16:51
  • Sure it is 1 question depending how you approach it. What do I need to do to my peace of code to be able to .append it or .innerHTML with out having to make it into a string first. – IamCavic Sep 15 '17 at 16:54
  • I believe the exact approach you want to take is not possible, because these methods are supposed to deal with strings. However, i made an edit to my answer regarding the usage of multi-line strings. – doubleOrt Sep 15 '17 at 16:59
  • Back ticks worked I am happy with that solution.. I can just remove the back ticks while I am writing code so I can see what I am doing.. and put them back when I need to make it execute.. that works well enough – IamCavic Sep 15 '17 at 17:09