Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inject javascript functions on server asp.net/php

I created a small javascript library that makes making ajax calls using POST and JSON much easier for webmethods. Right now I still have to make a function stub in javascript for each matching webmethod. What I would love to do in asp.net mainly, and if possible in php too, would be that before sending the page to the client, the server examines the exposed webmethods and automatically injects the stubs in the javascript on the page before it sends the page to the client.

In ASP.NET I'm sure there is some way to use reflection to find these and then some way to inject javascript to the page before sending. I'm sure I could figure out the reflection part, but not sure how to inject javascript to the page from the server. Does anyone know how to inject javascript to a page on the server side?

Also, does php have a way to sort of reflect on it's functions to also do this? Would be cool if this solution could work on multiple platforms.

like image 954
user441521 Avatar asked Jun 12 '26 00:06

user441521


1 Answers

You can use:

ClientScript.RegisterClientScriptBlock(GetType(), "FunctionName", "code", true);

also you check if method is already present or not with:

ClientScript.IsClientScriptBlockRegistered(GetType(), "FunctionName");

If you want to do so, you can even insert the reference to a postback (or callback):

ClientScript.GetPostBackEventReference(..)
ClientScript.GetCallbackEventReference(..)
like image 77
SmartK8 Avatar answered Jun 13 '26 14:06

SmartK8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!