Webpage not displaying from the beginning of the page - javascript

So I have a webpage that doesn't load from the top of the page. When the webpage loads, it automatically goes to an image. I think I am experiencing this because of two issues.
I think it has something to do with the line
$( 'html, body' ).animate( {scrollTop: $( '#' + image_container.attr( 'id' ) ).offset().top }, 500 );
Can someone explain what it's doing? I'm not sure how the scrollTop is working in conjunction with animate and the offset() function.
While I was tracking down the line above, I experienced something strange with the following code.
#In file 1
console.log("1")
console.log(function1())
console.log("2)
#In file 2, this is where the line of code from the first part of my question resides
function1(){
some_code_here;
return true;
}
I wanted to see exactly what my question for part 1 was doing so I did a console.log before and after the function while also doing a console.log on the function itself. The console prints out 1 and 2 fine. However when it comes to function1() I get undefined. I was expecting true. I actually added the line return true myself to file 2 because it was giving me undefined. However by adding the return statement I still got undefined.
Since the google chrome inspector is telling me undefined instead of an error, I know that function1() has been accessed, but why is it not printing true?
Lastly, in an attempt to try and stop the webpage from displaying at where the image is (rather than at the beginning of the webpage), I tried window.scrollTo(0,0) in file 1 but that doesn't seem to work for me unless I comment out the call to function1().
Does anyone know what I'm missing?

Turns out the problem to 2 was because the chrome inspector wasn't updating the javascript that I was editing. I had to go chrome inspector then to the Network tab and check Disable cache.
You can check whether your javascript is being updated by going to the same Network tab and select the js file.
I also restarted the server after that but I think by disabling the cache would have done it.

Related

Firefox extension: invoking function from webpage js

I'm trying to automate some boring, duplicated data entry into a web application at work.
I'm sorry if the answer is well known but I've spent hours in google trying to figure it out.
The problem is, in the web-based application none of the links are simple HTML, 'a href' type links, they use javascript.
I can see in one of the .js files for the website that the function invoked when a link is clicked is defined as follows
function sendEvent(eventtype, targetid, value)
...and with the Firefox debugger I can see that in order to do the simple page navigation I want to do, my extension has to invoke the websites js function as follows (the 'value' parm can be null)
sendEvent("click", "mx709")
I found this similar question. The suggested method is
content.wrappedJSObject.funcFromPage()
or
getBrowser().contentWindow.wrappedJSObject.funcFromPage();
... but running either of those lines in my extension doesn't seem to invoke the function and hence "click" the link I want clicked
EDIT 1 - in case it wasn't clear, the code I actually put into my extension was:
content.wrappedJSObject.sendEvent("click", "mx709[R:3]");
EDIT 2 - Also tried this, no dice. I have the Firefox debugger open, and a breakpoint on the top of the 'sendEvent()' function. Every time I click a link in this web app, I hit the breakpoint, when I try lines like the above (or the following), the breakpoint is not tripped
window.content.document.defaultView.wrappedJSObject.sendEvent("click", "mx709[R:3]");
The wrappedJSObject method should work.
Open up scratchpad and copy paste this:
var indexOfTabWithMyDocument = 1; //1 means 2nd tab, 0 is first tab
Services.wm.getMostRecentWindow('navigator:browser').gBrowser.tabContainer.childNodes[indexOfTabWithMyDocument].contentWindow.wrappedJSObejct.FUNCTIONTORUN()
Set the environment of scratchpad to browser. And run. It will work.

JQuery selector not working intermittently

I am writing a crawler which goes through pages if it detects a ">" type button on current page. Code fails about 1 time out of 100 with error "Uncaught TypeError: Cannot read property 'innerText' of undefined". In attempt to troubleshoot I open Chrome's developer console and test the selector on that specific page manually. It always returns what I expect and the button is visually there. I am puzzled why it fails only rarely and at the same time page where it fails looks normal. Any suggestions how to isolate the problem?
Code:
lastButton = $(".button a").last()[0];
if(lastButton.innerText === ">") {
next(lastButton.href, "getFriends", id);
}
At what point does your crawler run that selector? If the page isn't completely finished rendering yet, because the DOM isn't ready, or the code on the page maybe hasn't finished building out the page, your call to $(".button a").last() would return an empty array. Since you can access any index off that empty array still, when you try to grab the first item out of it you'll get undefined, which would then result in an error when you try to call .innerText off of it.
Try logging how many items are being returned by the selector to see if that is the case.
Found that code in question actually works fine 100% of time. The problem is somehow related to my crawler executing a different scrape function without loading the page for that function. So the peculiar intermittent problem is somewhere else.
Thank you for all who tried to help!

FancyBox Jquery Null Error - Strange issue

I am experiencing a very strange issue i am hoping someone can answer (its kind of broad, but i will explain it my best), code works on local page when opened, but as soon as the same page is uploaded it throws an error. Two others have also looked at this, resulting in more confusion.
I downloaded FancyBox to use the inline feature to pop up a user form via href link, instead of taking the user to an additional page.
You can see the "stock" fancy box here:
1 fancyapps.com/fancybox/demo
Under various options, Inline is what i am using.
I didnt need all of the other features that came with FancyBox so i stripped the page to the following, which works just fine:
2 *Fancy Box Demo Stripped to Inline Feature only*
So then, i applied this code to our sandbox copy of the page to implement:
3 *Sandbox Copy with Fancy Box inline feature added to "make offer" link*
It stops working! There are no conflicts with other javascript on the page, and the only difference is that it has a couple of color .css changes, all .fancybox was named to .ptroffer and that the css code is not inline on the page (which wouldn't cause this error anyway).
This same page, opened locally works beautifully - upload and it throws error.
SCRIPT5007: Unable to get value of the property 'ptroffer': object is null or undefined
<script type="text/javascript">
$(document).ready(function() {
$('.ptroffer').ptroffer();
});
</script>
Works fine with link #2, and with link #3 locally, but with #3 uploaded it throws error.
Please!
Something in your code somewhere is overriding $. If you change that "ready" handler to call
jQuery('.ptroffer').ptroffer();
you won't get that error. However, things may not work; I think that an older version of jQuery is being imported by something (1.3!), which is bad.
edit oh I see, something's pulling in Prototype. You've got a regular script soup going on there, and things are going to be unpredictable and bizarre until you get that straightened out. Probably somewhere in there something's calling jQuery.noConflict(), but that "ready" handler you're adding isn't written to expect that. Whatever code that's expecting jQuery 1.3 may be in for a surprise also.

javascript to launch only ONE window for a Java applet with a given URL

I need a javascript solution to launch only one window, with a Java Applet in it, for a given URL. I found a solution posted here on Stack Overflow -
here: JavaScript window.open only if the window does not already exist
But it doesn't seem to work .. I get
Error: launchApplication.winrefs is undefined
Line: 29
I can't seem to post the code in this little box and make it look right below,
so the code (my working code, plus the solution from above)
is here: http://pastie.org/833879
Where is the error?
As I understand it, the hash or array or whatever I use to store the called references to the windows opened this way will be lost if the calling window is closed.
Is there a way to make this work even if the calling window is closed and reopened? To basically ask the browser: "Do you have a window open with the following URL?" and if so, "What is the reference to that window?" (so I can raise it).
First Problem [Resolved]
I see that you're using winRefs in one spot, and later winrefs. It may just be the case that you need to uppercase the R on the second. It appears that you've got the lowercase version in your code a few times, even after line 29. Be sure to update all of them before testing again.
Second Problem
I just noticed that this line:
if ( typeof launchApplication.winRefs == undefined )
Will not return true if .winRefs is absent. Of course we want it to return true if the property is not found so we have to wrap undefined in quotes to get this working the way we want:
if ( typeof launchApplication.winRefs == "undefined" )

JavaScript error in Safari, only sometimes

I have a webpage that is using jQuery to hide divs on the page load and show them later based on user interactions.
In my $(document).ready() I execute a bunch of code to hide these divs and to bind a function to the click() handler from jQuery for the regions that trigger showing these divs. It also grabs some values out of the HTML to be used by scripts later. The latter is what's causing an issue.
This code works fine in Firefox and Chrome/Chromium (we're still working on the CSS for IE, but the JS works as far as I can tell). In Safari, it works flawlessly about 70% of the time. Every few page loads however, a line in my $(document).ready() gives me an error and stops the JS from executing, also halting the drawing of HTML for the rest of the page.
the line is:
var itemCount = document.getElementById('itemCount').innerHTML;
The debug console in Safari says "Null Value". The thing is, I see the following in my HTML (from the "view source" of the page after it failed to load right):
<div id="itemCount" style="display:inline">0</div>
and it is the only item with this id (obviously.)
I'm thinking that somehow the JS is getting run before the document is actually ready, and was thinking I'd try testing to see if document.getElementById('itemCount') returns null and wait for a bit if it does, but I don't know if this would work, or if there is a less ugly solution.
Let me know if I'm missing something obvious, or being dumb some other way.
From the way your code is written, I think there must be some other error on the page that is causing this. Your first code block should be:
var itemCount = $('#itemCount').html();
...and the second:
<span id="itemCount">0</span>
A <div> set to be displayed inline is a <span>. A <span> set to be a block-level element is a <div>. That's the only reason there are the two tags. They're otherwise identical. Use the right one for the task.
Not that I expect either of these changes to change your symptom. I just suspect you have other...questionable things on your page, and that's what's really causing the problem. Wild guess: move the <script> block containing the ready() handler to the bottom of the document's <body>.
If you're not already using Safari 4, by all means do so. Turn on the Develop menu in the advanced preferences, then say Develop > Show Web Inspector before loading your page. If there are errors, it will do a better job of showing you why than Safari 3.
Seems to be an old bug. See ticket 1319 and ticket 4187.
See this potential workaround:
After some experimenting and deleting 99% of this post :) - adding an empty style tag dinamically magically fixes the problem:
(function(){
if (!/WebKit/i.test(navigator.userAgent)) return;
var el = document.createElement("style");
el.type = "text/css";
el.media = "screen, projection";
document.getElementsByTagName("head")[0].appendChild(el);
el.appendChild(document.createTextNode("_safari {}"));
})();

Categories

Resources