I have an ASP.NET web application and I want to inject a JavaScript SDK to several selected pages during the page load of one specific page(for example HomePage.aspx).
I was thinking of a method like this to call on Page_Load of HomePage.aspx
injectScript(list of selected pages, script){
for each page in selected pages{
page.embed(script)
}
}
But can we use Page.ClientScript.RegisterStartupScript() to register scripts for other pages while calling it from HomePage.aspx?
Is there any better way?
Update 1:
In nutshell, All these pages share a master page but adding this JavaScript SDK to master page will make the SDK available to all child pages. I want to have a way to toggle(on/off) this SDK in each page based on user input(saved in a database).
There are more than 30 aspx pages and adding a method to page_load in each page is not feasible.
Is there any simple way to do achieve this functionality?
In case if anyone wondering the same, here is the solution I came up; use the following method inside Page_Load of the master page.( You will have to populate the list with appropriate data-source)
private void EnableSDK()
{
List<string> pages = new List<string>(); //selected aspx page names(not class names)
foreach (string page in pages)
{
if (page == System.IO.Path.GetFileNameWithoutExtension(Page.AppRelativeVirtualPath))
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "somekey", "somescript", true);
}
}
}
Related
What's the best way to handle this scenario ?
Code wise have I would like to have 2 seperate GWT modules. Modules as defined in this page
Download both modules in a single download, as a js file.
Run one of the modules in a dynamically created iframe.
Detailed description:
I have a GWT module called 'embed' which generates embed.nocache.js file.
I have another GWT module called 'widget' which generates widget.nocache.js file.
I add embed.nocache.js to my HTML page. This adds a link called 'widget' in the HTML page.
On click of this link, an iframe (say, widget.html) opens. widget.html has a link to widget.nocache.js. This file gets downloaded, gets executed in the iframe and puts a horizontal panel into the iframe.
Now I need to eliminate a seperate download of widget.nocache.js file.
Say I inherit 'widget' module in embed, it gets compiled and downloaded together. How do I initialise only the 'widget' related javascript in a dynamically created iframe ?
Can creating a custom linker help ?
Something like this. If you already have an IFrame on your page then just load it into that. The module will run fine so long as it isn't a Cross Site page and belongs to your own domain.
Document doc; // iframe document content
final String url_base = GWT.getHostPageBaseURL();
final Frame frame = new Frame(url_base + url_embedded); // your IFrame
frame.addLoadHandler(new LoadHandler() {
#Override
public void onLoad(LoadEvent event) {
doc = IFrameElement.as(frame.getElement()).getContentDocument();
wrapIFrameContent(); // If you need to wrap any widgets
}
});
frame.setStyleName("css_style_name");
RootPanel.get("content").add(frame);
And that's basically it.
You can embed multiple pages too.
This is just an alternative to Window.Location.assign(url)
Wrap widgets from IFrame pages like this:
Button add_module = Button.wrap(doc.getElementById("addmodule")); // button
Element delem = doc.getElementById(id_events_div); // DIV element
events_div = DivElement.as(delem);
events_list = ListBox.wrap(doc.getElementById(id_event_list)); // ListBox
I'm using Telerik UI for asp.net. Specifically I'm using RadTabStrip with partial page postbacks to allow the user to tab through different sets of data. When the user clicks a tab, some code executes and loads data just for that tab.
I've figured out how to execute codebehind: I set the OnTabClick property of the RadTabStrip, and then in codebehind I check what tab was clicked.
E.g.
protected void tab_Click(object sender, RadTabStripEventArgs e)
{
if (e.Tab.Text == "Info")
{
populateInfoTab();
}
}
private void populateInfotab()
{
// Do some stuff
}
However, I can't figure out how to execute client side javascript after a specific tab is clicked. What I tried:
Set OnClientTabSelected property, and then add some javascript:
function tab_ClientClick(sender, args)
{
var tab = args.get_tab();
if(tab.get_text() == "Info")
{
alert("Tab Clicked");
}
}
The problem is that I need to set the InnerHtml of some div in the clicked pageview after it is clicked. The div does not exist on page load (that specific RadPageView is hidden) so I cannot set it then. Once the user clicks into the tab, and after the page view loads, I need to be able to update the div's InnerHtml through JavaScript.
How would I go about doing this?
First option - if you do not set the RenderSelectedPageOnly property to true, all page views will be rendered on the initial load and you will be able to use JS to find/modify elements in them.
Second option - just set the content from the server as soon as you load the UC, this will usually make things simpler.
Third option - use client-side events (offered by the native PageRequestManager class or the RadAjaxManager, depending on how you setup your AJAX interactions) to execute when the response is received. The difficulty here is to determine which is the postback you need. Looking for the desired element and only executing logic if it exists is the simplest flag you can opt for.
Fourth option - register a script from the server code that will call your function, something like:
populateInfoTab();
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "someKey", "myDesiredFunction()", true);
where you may want to use the Sys.Application.Load to ensure it is executed later than IScriptControl initialization.
When I am clicking back button from browser it is showing the outdated value from database. I tried to use following code but it's not working
<script type="text/javascript">
$(document).ready(function() {
alert("I am an alert box!");
});
</script>
How to refresh the page or get updated value when users click on back button of browser?
Asp.Net Webforms:
In Page Load (or) if you want all pages then put on Master Page Load
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
Asp.Net MVC:
Add this attribute in Action or Controller
[OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
Or
If you want disable cache for all views then add the below in Filter Config
filters.Add(new OutputCacheAttribute{VaryByParam = "*",Duration = 0,NoStore = true});
Edit: I noticed, in comment you mentioned you are using ruby on rail. I am not familiar with that, but the logic is server need to send an header to the browser for "do not cache that page". So you can find some answers here Ruby on Rails: Clear a cached page
Use a hash to determine if it has already been refreshed, to ensure it happens just once:
window.onload = function() {
if(!window.location.hash) {
window.location = window.location + '#loaded';
window.location.reload();
}
}
There is no problem with the integration of jquery and the code. Rails project comes with TurboLink which have some conflicts with jquery. One of the possible solution is to remove turbolink from the project (remove it from application.rb) and use Jquery-Turbolink.
This will allow to use jquery inbuilt functions like I had in my question.
Please update the answer or add comment for other possible solutions.
In my project am using a master page named as master.aspx in particular page i need to include another page into the current page, so that i use <iframe> to achieve this. while passing the url to the iframe the master page will also load inside the <iframe> how can i avoid such loading of master page inside the iframe?
i can't avoid master page in the page which i called, is it possible to call particular <Div> or <content template> in iframe?
seems you are using asp.net, maybe you can try to edit the first row in .aspx page, try to add the following code to the page which you don't want to show the master page.
Theme="" StyleSheetTheme=""
==== Update ====
if you want to hide it in iframe only, you should pass a parameter in url like ?theme=none
then handle it in the Page_load Function in .aspx.cs as follow:
protected void Page_Load(object sender, EventArgs e)
{
if(Request["theme"] == "none"){
Page.Theme = "";
Page.StyleSheetTheme = "";
}
}
can somebody please give me a hint here. I'm new to dealing with web page cookies, in another view (using MVC) I created a cookie with some basic info. Depending on that info I want to change the web page language. The cookie itself is created with JavaScript. I know how to solve this by creating the cookie in the controller, but I don't know which method is the best. Regards.
<script type="text/javascript">
{
if (document.cookie.indexOf("Russian") >= 0) {
// load partial view here
//#{Html.RenderPartial("~/Views/Home/About_.cshtml");}
}
else if (document.cookie.indexOf("English") >= 0) {
// load partial view here
//#{Html.RenderPartial("~/Views/Home/About_Eng.cshtml");}
}
}
</script>
You don't want to do that in JavaScript. In your controller call Request.Cookies["TheNameOfYourCookie"] then check the collection for the key. You can set it in on your model or set in to the ViewBag. Then when rendering your page check the value there and render the section.