0

Is there a way to pass in html to a .net function and have the result be the rendered output including JS changes? For example, can you do the following:

string html = Class.Method("<script>$('#test').show()</script><html><body><div id="test" hidden></div></body></html>")    

I used jquery for the example, but it can be traditional JS. Basically, the html variable would return the html I passed in, but the div 'test' will not be hidden anymore:

<html><body><div id="test"></div></body></html>

I've been researching JS engines in .net but I can't seem to find anything like this..

  • Possible duplicate of [Rendering HTML+Javascript server-side](https://stackoverflow.com/questions/8391133/rendering-htmljavascript-server-side) – freedomn-m Oct 11 '17 at 16:45
  • It sounds like it's a similar question. Thanks for your comment. Awesomium seems to be updating right now.. from that link. I'll do more research. – Matthew Carnaghi Oct 11 '17 at 17:05
  • If it's not for generic "run all js" and you have a specific requirement (eg hide `#test`) then you can render the html accordingly, eg instead of ` – freedomn-m Oct 11 '17 at 17:26
  • Yes, it'll need to be generic. We have several email templates (html that will be managed by us). So each template could have a different condition. Basically, i want to be able to say if (this) { show() } and render the output html, based on the condition, then send the email with the rendered output. – Matthew Carnaghi Oct 11 '17 at 17:34

2 Answers2

0

no because your js will be render inside the navigateur not before. if you want to be able to access it, you need to 'run' your page first, save the result inside a file and use this file.

Take a look at selenium or phantomjs. Or just make a call to the page

sheplu
  • 2,937
  • 3
  • 24
  • 21
  • To give you a little context, I'm trying to send the html output via email. There are some things I need to show conditionally. So, I'd like to get the html after js execution. I will take a look at your suggestions, and see if they help my situation, thank you. – Matthew Carnaghi Oct 11 '17 at 17:03
0

After look into this, there doesn't seem to be a good enough solution for this. So, I am going to implement RazorEngine to handle conditions and loops and return the html:

https://github.com/Antaris/RazorEngine

Update: The whole point of this was to render conditional statements and get the final html. Some people suggested browser classes which may have worked, but saving a template as razor to my db is much easier than implementing a browser class. Plus the overhead of sending thousands of emails could be taxing on performance. So, RazorEngine, which I didn't know was a potential solution when I wrote this question, ended up being my solution.

  • I looked at it, as well as PhantomJS. I just needed a way to handle conditionals before I send an email. Since we already use razor views, it was just easier to save a razor template and pass into RazorEngine for the html result. And it's not a lot of overhead. – Matthew Carnaghi Oct 12 '17 at 16:58
  • Your requirement says "including JS changes". I haven't checked out that RazorEngine but I don't understand how this thing is supposed to run JavaScript code. As far as I understand this requires an Internet browser or a library that run a browser in background like Selenium, WebBrowser class, etc. – derloopkat Oct 12 '17 at 17:57
  • Yes, as I stated in my answer, I found a way around having js in the template. And in my other comments I explained what I needed this for. RazorEngine does enough. I can pass in a string and it returns a string. Not grabbing from a view. I only thought js would be the way to go with doing conditionals. Now I can just use razor. – Matthew Carnaghi Oct 12 '17 at 18:41