Executing JavaScript on page load selectively - javascript

Mending a bug in our SAP BW web application, I need to call two javascript functions from the web framework library upon page load. The problem is that each of these functions reloads the page as a side-effect. In addition, I don't have access to modify these functions.
Any great ideas on how to execute a piece of code on "real" page load, then another piece of code on the subsequent load caused by this function, and then execute no code the third reload?
My best idea so far it to set a cookie on each go to determine what to run. I don't greatly love this solution. Anything better would be very welcome. And by the way, I do realize loading a page three times is absolutely ridiculous, but that's how we roll with SAP.

A cookie would work just fine. Or you could modify the query string each time with a "mode=x" or "load=x" parameter.
This would present a problem if the user tries to bookmark the final page, though. If that's an option, the cookie solution is fine. I would guess they need cookies enabled to get that far in the app anyway?

A cookie, or pass a query string parameter indicating which javascript function has been run. We had to do something along these lines to trip out a piece of our software. That's really the best I got.

Use a cookie or set a hidden field value. My vote would be for the field value.

This might be a cute case for using window.name, 'the property that survives page reloads'.

Related

Keep and run code in debugger every time the page is reloaded

I have a function that will reload the current page after a period of time. I want this function to run automatically every time the page is reloaded (using the debugger).
function reloadPage() {
window.location.reload(false);
}
setInterval(reloadPage,3000)
The problem is that every time the page is reloaded, the code in the debugger will be cleaned and the function will not be called. How to fix this?
A very simple solution: Rather than putting the Javascript into the console, consider putting it in your application but disabling it when you're not debugging.
For example, you could have a GET parameter in your URL that, when present, triggers the function. A good explanation of how to retrieve a GET parameter in Javascript is at How to retrieve GET parameters from javascript?
An even simpler alternative would be to simply leave this code commented out, and comment it in when you want to debug. (This is not a good practice and I will scold you for it during code review, but it is a real thing that real people do, and it has the advantage of being easy and working.)
-
An alternative: You could detect when the console is open, and only run your code when the console is detected (though this would annoy power users like me who tend to always have developer tools open). It's not trivial to detect, but there's a library you can use: https://github.com/zswang/jdetects

JS Page Searcher

I need to bruteforce through pages and check it's contents for certain text.
ie. I have page: http://example.whatever/internet?ip=$4.2$$.$$6.$$$ and those dollar signs, are missing numbers. I need to go through all of existing combinations untill 404 - Page not found text isn't present on that page anymore (it's not an error code, that gets returned, just the text.)
I also need to be able to use it when there is login required to access the pages, and I have the necessary access, that's why I suggest JS to be used in this, and not server side script, like PHP or Python.
Edit 1: In case you want to complain, that this is not a question, I'm asking how to do it. And if you don't have the balls (or whatever it is you have or don't have) to comment bellow, why you did press that arrow down next to this question, so we can resolve it, please don't do it. Thanks for understanding.
I'd recommend using something like PhantomJS for this. You could login then iterate through pages until you find a non-404 page.

submitting asp form by using JQuery ajax

Firstly, some short explanations for the pages that we will be using here in the example.
A.asp
The page where user interacts with various controls, fills the info, and submits the form.
B.asp
The page that creates session variables and stores values as well as serves as an intermediate page where it passes value / redirects to C.asp.
C.asp
The page that does processing and displays results.
So in short, the flow is something like, A -> B -> C
Now, what I'd like to achieve is to put the results right on A.asp instead of seeing them on C.asp. I guess I should be able to do it with JQuery Ajax, perhaps simply by using $("div#loadResults").load("B.asp").
But somehow I couldn't make it happen with above line of code. I think I must have missed something there. Can anyone kindly give suggestions or thoughts?
Thanks.
EDIT:
It is actually a massive Form, so I don't think it'd be good idea to upload huge chunk of code directly to here.
EDIT2:
Another question is: which of the following should I be using, Button.click(), Button.submit() or Form.submit()?

How to detect whether a browser enables javascript s.t. the master page can make a proper response?

I am developing a site using Asp.net MVC 3 with Razor.
In the _Layout.cshtml (the master page) I want to put a logic based on whether or not the browser enables javascript.
What is the simplest way to make this logic?
For the sake of simplicity, let the master page just output as follows:
#if(....)//need to modify
{ <p>javascript enabled...</p>}
else {<p>javascript disabled...</p>}
If you want to block the access of your application you can use something like this
<noscript>
<meta http-equiv="refresh" content="0;url=../Controller/Error" />
</noscript>
There's no way to find this out on the server, therefore there's no way to find out before the first page is loaded. The best you can do is to put a bit of Javascript into the page that sets a cookie or posts an AJAX response to the server telling it that Javascript is active, so you can do something about it on subsequent page requests. Even apart from the obvious problem of the first page load, it's a bad strategy since the user may switch off Javascript in the meantime while your server still thinks it's active.
Graceful degradation/progressive enhancement are the keywords here. Make your page assume by default that no Javascript is active and act accordingly, i.e. serve plain HTML in either case. Include Javascript that will "upgrade" the site's functionality if Javascript is active. Let the client figure out if Javascript is working or not and give it the means to work in either case.
I'm afraid there's no good solution. Almost all of the solutions out there somehow involve running a script to do the check and it doesn't feel right (at least to me). The best solution I can suggest is use the <noscript /> tag and redirect to a different page that does not depend on javascript.
Here is one trick...
Assume the user has JavaScript blocked (off). We put this code into the index.aspx:
<script>
document.location.href = "index.aspx?js=1";
</script>
If you get the js=1, you know that user has JS enabled.
So you can generate the code in according the user has / hasn't JS.
The other way is to generate contents witho some special class, e.g. <div class="noscript">, and then you run the script (jQuery):
$(".noscript").hide();

Run a JavaScript function from a URL

I need to link to a page that uses javascript to create the appearance of different pages. Each of the links on the page I am pointing to calls a javascript function that produces the "new" page. So, if I just link to the page, I get the default configuration. But what I need to link to is a particular configuration after the js function has run.
At first I thought I would be able to append the function to the url, but apparently that functionality is not supported for security reasons (is this correct?). Is it possible to post the values?
Does anyone know how I can display the correct configuration?
In the general case, no, it's not possible, which is why these sort of JavaScript-only pages are an inaccessible, unusable total pain in the neck and people should stop creating them, the idiots.
If you are lucky and the site you're talking about has actually Done It Properly, then they'll have used #fragment navigation, so when you click a link it does a history-able and bookmark-able navigation, updating the URL to one with a #something at the end that you can use to navigate back there. If you're really lucky, there might even be a fallback URL for non-JavaScript browsers that you could link to. But more often all there is is a JS function, which you can't link to or invoke outside of that site, leaving you quite out of luck should you want to deep-link anything.
(Did we learn nothing from the <frame> fiasco, guys? All you trendy webmasters hacking up clever-clever swooshy jQuery-animated load()-powered multiple-pages-in-one sites are churning out rubbish that is no better than the frame-addled worst of the Netscape 3 era. Stop it. Seriously.)
Okay, I was given the solution by a friend. It seems I should answer my own question. In fact, I felt a little silly once I saw how simple the solutions was. I just forgot how to plant "posts" in a URL. It seems the question itself was erroneous.
All I really needed to do was set some Javascript variables in a page I don't own. Solution looks something like this.
http://www.apage.com/control.aspx?var1=someVal&var2=someVal...&varn=someVal
Thanks to those who responded.
The owner of the page could do you a favour and provide a mechanism to run functions depending on a value in the querystring.
Otherwise there may be something that can be done with ajax but it could be messy.

Categories

Resources