Call view javascript function from javascript file - c#

I have a jquery template that is being loaded with values after the ajax call is done and not on the view load.
on page load my javascript calls and function from a script.js file. i need that script.js file to call another function from the view. How would i call the listload function from the script.js
var limit = 5, dir = ' Desc', sort = 'Created', index = 0, autoscroll = false;
function getDataUrl(index, action) {
return '/Team/Linking/ListItemLinks/#Model.ItemId/?type=#Model.ItemType';
}
function listload() {
alert('test');
}

Corey,
As per my understanding, you will invoke a function in a js file from a view and after that the js file will invokes another function residing in the same view.
I think you can achieve it (By theoritically) because,
When the page loads, it will clubs all the js.
Consider, if your js file points to a Master/layout page , when the inner page loads it will have the cumulative js (from page,outside js files and master/layout pages). So you can call the function residing in the page from js (need to verify).
I think jQuery may have similar kind of stuffs, because we are calling jquery using an external file, but after that it calls the next line.
Another thing is to check with the callbacks and events in javascript. I am not expert in
topic. You can check the following links, may be it helps you.
Create a custom callback in JavaScript
http://blog.pengoworks.com/index.cfm/2012/1/12/Adding-custom-callbacks-to-existing-JavaScript-functions

Related

c# check page has loaded before redirecting

I've have a page which currently does a small task then redirects to a 3rd party website.
The redirect is triggered by a Response.Redirect();
Now I want to add another function but this time it's javascript.
I'm using the following method to add the javascript to the page:
if (!page.ClientScript.IsStartupScriptRegistered(key))
{
ScriptManager.RegisterStartupScript(page, typeof(Page), key, script, false);
}
The function of the javascript is to track a sale, so I need the page to render it and trigger the track before moving on.
Anyone know if this will work or not, and if not how it can be achieved?
You're not going to be able to use Response.Redirect() to handle redirecting the user anymore. Instead, you'll have to redirect through JavaScript after your start-up script executes. I would simply tack on:
window.location.replace("http://www.whateveryourredirecturlis.com");
To the end of your existing start-up script.
Use something like this in your javascript.
document.onready = function () { window.location = "http://yoururl"; };

ASP.NET event that triggers both a javascript function then a c# function

I would like to accomplish an on click event to preform 2 functions sequentially
< onclick=" JavaScriptFunction() , C#ServerSideFunction()">
how would I go about this. I have found an example of how to do 2 javascript functions sequentially but not both.
What you'll essentially need to do is call a javascript function (which performs your client-side activities) which when finished makes your postback to the server.
In order for javascript to perform the postback you would use:
__doPostBack('<%=btnPlaceHolder.UniqueID %>', '')
as per this question.
Have it call a js function, and have that function call the js function you want first, and then call the C# function.
You can call a C# function from js using AJAX. Here's an example post about that:
Calling functions from an ASP.NET code file with javascript
And here's a whole blog post about calling C# from js:
http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/calling-an-Asp-Net-C-Sharp-method-web-method-using-javascript/

ajax asp.net and jquery conflict

I have multiple jquery files in my project.
Yesterday googled this problem and find solution. I edited this
$(document).ready(function () {
with this
function pageLoad() {
Now problem is that if i will use this in all my jquery files. Every scripts will stops working.
That is two different things. The
$(document).ready(function () {
is from and to jquery, it tells when the DOM is loaded and it start executing the code inside.
The
function pageLoad() {
is from ASP.NET and it is one of handler of Application.Load (client-side)
The reason why it will stop working is because in the same page you can only have one event with the same name, by default pageLoad will be the handler of Application.Load but you can associate another name. I don't see why you have problems between the two, can you explain it better?
EDIT:
After your comment, if you want/need to use Sys.Application.add_load (by default pageLoad) you should add a different name for each js file you need.
From msdn:
// Attach a handler to the load event.
Sys.Application.add_load(applicationLoadHandler);
function applicationLoadHandler() {
// Redirect to alternate page if not business hours.
var d = new Date();
if (!(8 < d.getHours() < 17)) {
window.location = "AfterHours.aspx";
}
As you can see, you can give a different name to add_load and you should give a different name to use more than one for the same page/request.
}
Replace pageLoad with something like this:
$(function() {
// This will be executed when the page is loaded
});
or to avoid name collisions...
(function($) {
// This will be executed when the page is loaded
})(jQuery);
Edit: As rich.okelly pointed out the second example will run as soon as the script is loaded.

Sys.WebForms.PageRequestManager pageLoaded event caching function?

I've got a master page that contains this code at the bottom of the page:
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(closeLoading);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(parseData);
</script>
</form>
the parseData() function I'm creating inside the main page and I'm adding functions to it based on each page load. parseData IS called after every AJAX refresh, but it appears to only call the contents of the function from BEFORE the request. If I hit F5 to refresh the page again, it will properly call all of the newly added content in the parseData function.
Does this function cache that data? How can I make sure it calls the newly created contents of the parseData function?
According to the research I did, this is a problem with MS AJAX UpdatePanels and you are really better off not using them. I revised my JavaScript to pull data from hidden form fields instead so the JavaScript functions never changed and was able to get this working.

Run ajax scripts on page when navigating with ajax?

I got a bit of an issue in my ASP.NET MVC project.
I have a chat div in the bottom right corner (like facebook), and of course I do not want this to reload when navigating to all my navigation is ajax.
The problem I am facing is that I use the following code on the top of the view page:
<script type="text/javascript">
$(document).ready(function() {
$('#divTS').hide();
$('a#showTS').click(function() {
$('#divTS').slideToggle(400);
return false;
});
});
</script>
The problem is that this code is only loaded with ajax and does not seem to fire? I would like to run all scripts in the newly loaded view, just as if I hadn't navigated with ajax.
I cannot put this in the site.master as it only loads once and then probably the divs I am trying to hide doesn't exist.
Is there a good way to run scripts in the ajax-loaded div?
You will need to run the scripts in the success callback function of your ajax script. I would recommend you externalizing this into a separate function:
function setupEffects() {
$('#divTS').hide();
$('a#showTS').click(function() {
$('#divTS').slideToggle(400);
return false;
});
}
$(document).ready(function() {
setupEffects();
});
And in the success callback of your script call this function:
success: function(result) {
setupEffects();
}
The Masterpage gets reloaded when you navigate to another View. You can also check if a div exists with $('#div').length > 0.
By the way, "full site" ajax navigation should not be used. Reload your chat on navigation - best put it into a control (makes things easier).

Categories