Backbone JS fire event upon rendering template - javascript

I'm using underscore.js template function to render templates in my views
<script type="text/html" id="friends-svg">
<%% if (this.model.get("returning_user")) { %>
<svg></svg>
<%% } else { %>
<div id="message-container">
<h3>You are a new User. Please wait while your Facebook Data is Loaded</h3>
<div id="progressbar"></div>
</div>
<%% } %>
</script>
For example the above code renders a div that I will target with jquery to create a progressbar if the user is not a returning user, and an svg element will be rendered that I will target with D3 if the user is a returning user.
The problem is that I believe my functions for creating the progressbar or for making the D3 graph are being called before the html in the template has been appended to the DOM. For example my plotData function is being called when the collection is fetched
this.listenTo(this.collection, "reset", this.plotData);
I have played around with where to call the function
this.createProgressbar()
But essentially I think I need to somehow create a custom event listener for when certain elements such as svg or #progressbar div are appended to the DOM. Is it possible to create a custom event/s for when these elements are appended to the DOM?

You may be interested in waitForKeyElements.js written by BrockA. Although the he has written it for userscripts, it may be helpful. It listens for the element and when it is added to the DOM it runs the function. That is what you are asking for but I don't think it is the best way to get what you want. I personally would just call the function that you would put in waitForKeyElements after your element has been placed in the DOM. In a userscript you can't do this because you can't just insert a function anywhere in the execution of the page. That is why this was create for userscripts and not just any script.
So if you went with waitForKeyElements you would have something like this:
waitForKeyElements ("svg", function(){doStuffToSVG});
waitForKeyElements ("#progressbar", function(){doStuffToProgressbar});
So when you run the code that inserts whichever element on the page it will call the function. This is almost the same as doing something like this:
<head><script src="js/progressBarFileToLoad.js"></script></head>
<body>
...
<svg></svg>
<script type="text/javascript">doStuffToProgressbar;</script>
...
</body>
But the waitForKeyElements way has an interval (setInterval) running every 300 milliseconds that is completely avoidable by the latter example. Not to mention the extra work that goes into clearing the other interval when you realize it isn't needed anymore and all the other stuff that you will need to do.
If you code is running before the element is added to the page then place it in your html after the element is added to the page. If for some reason you can't change when the element is added to the page and you can't change what all is added to the page then I guess listen for when it is added.
Also, if you decide to go with waitForKeyElements, be sure to read the comments in that file. It shows how to change the interval time and the iFrame to search for the element which could greatly improve performance.

Related

How do I change the title of a web page using javascript?

I know this is a duplicate question, but I've tried a few approaches and I'm not able to get the solution I need.
I need to change the title of a web page, and I thought Javascript would be a good candidate. I've read many disapproving comments already, talking about how changing the title will negatively affect SEO-I'm not worried about that just now.
I'm able to change the title by reassigning a value in an inline script:
<input type="button" value="Click me." onclick="document.title = 'Some new title';" />
But using an inline script in this case is out of the question. I tried loading an embedded script tag above & below the body of the script, no go. This is what I settled on, and it didn't work initially (keep reading):
<script>
document.addEventListener("load", function changeTitle(){
document.title = "FUBAR";
}, true);
</script>
I've tried adding/removing the 'true' value at the end of the parameter list and that doesn't change anything. I avoided naming the function, then named it, and that didn't change anything. What DID work was changing "load" to "click". I need the title to change right after the document is finished loading...is there something else I can use, like "ready", or "onload"? Using "load" yielded no results, but I swear I've seen it used before.
Thanks!
Try using
window.addEventListener rather than document.addEventListener
See
https://developer.mozilla.org/en-US/docs/Web/Events/load
Note: More reliable is to add event listener on
"window.addEventListener".
No need to wait for the load event. Just set the title:
<html>
<head></head>
<body>
<script>document.title = "foobar"</script>
<!-- rest of document -->

Execute a second function when a first function completes W/O a callback parameter

Background: I'm running A/B tests for a website via VWO. I can write a script that is run when the page loads, but I cannot access any of the website's preexisting scripts or alter their code in any way besides "overwriting" in JS (e.g. remove divs, change CSS properties, append divs) after their page loads.
Problem: When the page loads, there is an empty <div id="priceinfo"></div>. Elsewhere on the page, there is an <img ...>. Inside RequestPriceInfo(), the function outputs HTML to div#priceinfo based on certain form field values on the page. It also appends an "order" button (which is actually not a button at all, but an image nested in anchor tags). I'm trying to change the source for the "button" image using JQuery's attr() function.
Progress: None. I have tried using $('a#requestPrice').click(function() {...} ); but it is not working to change the source of the image, I presume because the content has not yet loaded onto the page immediately when a#requestPrice is clicked and my function runs. I need a way to tell when RequestPriceInfo() has been fired and completed, but there is no callback parameter on RequestPriceInfo() and I don't have access to the script to alter it and add one.
Potentially Useful Info: There is a variable, priceRequested, which is changed from false to true when RequestPriceInfo() runs. Though I realize nothing about this solution is going to be elegant, it seems unreasonable to use Object.prototype.watch() to monitor the variable, but I'm running out of ideas.
How can I detect when RequestPriceInfo() has completed to execute my function without editing the function to add a callback parameter?
What about using CSS... your script could add a class to <div id="priceinfo"></div>, like maybe <div id="priceinfo" class="newButton"></div>. Then, in the CSS, you'd hide the img for #priceInfo.newButton and insert your new image as a background image for the anchor.
Here's one way
var oldRequestPrice = RequestPriceInfo;
RequestPriceInfo = function(){
oldRequestPrice();
// Function completed, handle here
}
EDIT
As the question seems to imply that RequestPriceInfo is an asynchronous API call, have a look at this question: Add a "hook" to all AJAX requests on a page
This will keep track of all Ajax requests happening on the page. You can pick out the one you need and fire your function/code on its success.

JavaScript code not running

I have the following code:
...<some code>...
...<elements load>...
<script type="text/javascript">
var selected_city = document.getElementById('jform_city').value;
var selected_province=document.getElementById('jform_province').value;
<!-- set the onchange property for all options in provice to activate getCities() -->
document.getElementById('jform_province').onchange = function() {
getCities();
}
if(selected_province != ''){
getCities();
}
</script>
...<more elements load>....
<script type="text/javascript">
alert("TEST");
document.getElementById(selected_city).selected="1";
</script>
It selects an option from a drop down list I have, the problem is, if I remove the alert("") it stops working for some reason, any ideas?
You need to wait for the document to load before trying to use document.getElementById. The alert is slowing things down enough that, behind the alert box, the element gets loaded. Without the alert, there may well be no element for "selected_city" when the following line gets run.
Check out this StackOverflow question for more info about waiting for the page to load.
EDIT
First of all, what happens when a web browser parses the page is that it translates the HTML elements it receives into a DOM - the Document Object Model, which is the in-memory representation of the page. Only after the element is in the DOM is it possible to manipulate it, and it is not necessarily true that the elements will enter the DOM in the same order as they appear in the HTML: modern fast browsers run very asynchronously, only guaranteeing that the page will end up looking as if the whole thing was loaded synchronously. This is easy to verify: almost every browser will load text and even display it while it goes and fetches images. Only after the image is fetched does it insert it into the display, shoving / reflowing the text if it has to. The end result is the same, but the page appears to load much faster this way.
Unfortunately, this "wait for it" guarantee does not apply to Javascript (as JS itself is allowed to change the way the page loads). Therefore, simply moving the script tag to the end of the document is not the same thing as waiting for the DOM to contain the element.
You have to actually wait for the browser to call you back and tell you that the DOM has been loaded - that's the only way to really know you can manipulate the DOM. This is what window.onload is for, this callback. I can think of a couple of reasons this isn't working for you:
If you actually just plugged in verbatim window.onload = function();, you missed the point - this is meant to not be an empty function, but your function. I'd assume that you didn't just type that in, but just in case, your code should be
window.onload = function(){
document.getElementById(selected_city).selected="1";
};
Alternatively, since window.onload=[some function] is assigning a function to be called later to one single variable, there can be only one. If you're loading some other script that also assigns window.onload, your callback can be lost.
This is why frameworks such as jquery, which has a ready function that can accept and call back any number of functions, are frontend developers' gold. Here is another StackOverflow question specifically asking about using onload.
Finally, this line:
var selected_city = document.getElementById('jform_city').value;
also requires the DOM to be loaded before it runs properly. selected_city itself could be null or undefined because #jform_city is not loaded when you're asking for its value. This in turn will cause document.getElementById(selected_city) to fail, even if that element is loaded at the time that you try to select it.
Asynchronicity is a real pain in Javascript.
Any time you need to get information from the page itself, as a rule of thumb, you must wait for the DOM to load. In practice, this means that almost all of your code (except that which does not on any way require the page to be loaded) should be in a function that gets called after the page is loaded.

Javascript execution order

I want to give a static javascript block of code to a html template designer, which can be:
either inline or external or both
used once or more in the html template
and each block can determine its position in the template relative to the other javascript code blocks.
An example could be image banners served using javascript. I give code to template designer who places it in two places, once for a horizontal banner in the header and once for a vertical banner. The same code runs in both blocks but knowing their positions can determine if to serve a horizontal or a vertical image banner.
Make sense?
Another example: Say you have the same 2 javascript tags in a web page calling an external script on a server. Can the server and/or scripts determine which javascript tag it belongs to?
NOTE: Can we say this is a challenge? I know that I can avoid this puzzle very easily but I come across this on a regular basis.
JavaScript code can locate all <script> elements on the page and it can probably examine the attributes and the content to check from which element it came from. But that's probably not what you want.
What you want is a piece of JavaScript which replaces tags on the page with ad banners. The usual solution is to add a special element, say a IMG, for this and give that IMG an id or a class or maybe even a custom attribute (like adtype="vertical") and then use JavaScript to locate these elements and replace the content by changing the src attribute.
For example, using jQuery, you can should your images like so:
<img src="empty.gif" width="..." height="..." class="ad" adtype="..." />
Then you can locate each image with
$('img.ad')
[EDIT] Well, the server obviously knows which script belongs into which script tag because it inserts the script. So this is a no-brainer.
If the script wants to find out where it is in the DOM, add something which it can use to identify itself, say:
<script>var id= '329573485745';
Then you can walk all script tags and check which one contains the value of the variable id.
If you call an external script, then you can do the same but you must add the ID to the script tag as you emit the HTML:
<script id="329573485745" src="..." />
Then the external script can examine the DOM and lookup the element with this id. You will want to use an UUID for this, btw.
This way, a piece of JS can locate the script tag which added itself to the page.
Best thing would probably be to make an insert once function, and then have him insert only the function call where needed.
Like this:
timescalled=0
function buildad(){
var toinsert="" //Code to generate the desired piece of HTML
document.write(toinsert)
timescalled+=1 //So you can tell how many times the function have been called
}
Now a script block calling the function can simply be inserted wherever a banner is needed
<script type="text/javascript">buildad()</script>
Thanks for the tips everyone but I'll be answering my own question.
I figured out several ways of accomplishing the task and I give you the one which works nicely and is easy to understand.
The following chunk of code relies on outputting dummy divs and jQuery.
<script>
// Unique identifier for all dummy divs
var rnd1="_0xDEFEC8ED_";
// Unique identifier for this dummy div
var rnd2=Math.floor(Math.random()*999999);
// The dummy div
var d="<div class='"+rnd1+" "+rnd2+"'></div>";
// Script which :
// Calculates index of THIS dummy div
// Total dummy divs
// Outputs to dummy div for debugging
var f1="<script>$(document).ready(function(){";
var f2="var i=$('."+rnd1+"').index($('."+rnd2+"'))+1;";
var f3="var t=$('."+rnd1+"').length;";
var f4="$('."+rnd2+"').html(i+' / '+t);";
var f5="});<\/script>";
document.write(d+f1+f2+f3+f4+f5);
</script>
Why not not just place the function call on the page instead of the entire code block? This way you can pass in a parameter to tell it what type of advertisement is needed?
BuildAd('Tower');
BuildAd('Banner');
Javascript itself has no clue of it's position in a page. You have to target a control on the page to get it's location.
I don't think it is possible for JavaScript code to know where it was loaded from. It certainly doesn't run at the point it is found, since execution isn't directly tied to the loading process (code usually runs after the whole DOM is loaded). In fact, in the case of externals, it doesn't even make sense, since only one copy of the code will be loaded no matter how many times it is encountered.
It shouldn't be the same code for each banner - there will be a parameter passed to whatever is serving the image banner which will specify the intended size.
Can you give a specific example of what you need this for?
To edit for your recent example: The simple answer is no. I could help you approach the problem from a different direction if you post details of your problem
The term "static block of code" leaves a lot of room for interpretation.
Inline scripts (e.g., ones that rely on document.write and so must be parsed and executed during the HTML parsing phase) cannot tell where they are in the DOM at runtime. You have to tell them (as in one of the first answers you got).
I think you'll probably find that you need to change your approach.
A common way to keep code and markup separate (which is useful when providing tools to HTML designers who aren't coders) is to have them use a script tag like so:
<script defer async type='text/javascript' src='pagestuff.js'></script>
...which then triggers itself when the page is loaded (using window.onload if necessary, but there are several techniques for being triggered earlier than that, which you want because window.onload doesn't trigger until the images have all loaded).
That script then looks for markers in the markup and manipulates the page accordingly. For instance (this example uses Prototype, but you can do the same with raw JavaScript, jQuery, Closure, etc.):
document.observe("dom:loaded", initPage);
function initPage() {
var verticals = $$('div.vertical');
/* ...do something with the array of "vertical" divs in `verticals`,
such as: */
var index;
for (index = 0; index < verticals.length; ++index) {
vertical.update("I'm vertical #" + index);
}
}
The designers can then have blocks on the page that are filled in by code which they flag up in a way that's normal for them (classes or attributes, etc.). The code figures out what it should do based on the classes/attributes of the blocks it finds when it runs.

Manipulating <body> with jQuery? (Or figuring a better solution)

I'm trying to create a JS-script to make modifications to add a footer to HTML -documents on the fly. The idea is to append a div-element at the end of the document to contain the footer, and to provide a floating fixed footer, I also need to have all of the other content wrapped in a div, basically I need something like this:
<html>
<head>
<title>Foobar</title>
</head>
<body>
<div id="contentWrapper">
<!-- Content is here -->
</div>
<div id="footerWrapper">
<!-- Footer goes here -->
</div>
</body>
</html>
The problem is, that the HTML is generated from a system where the end user's have had a little too much control over the structure (it's a blogging platform), and there's no guarantee of a certain sturcture hence I need to wrap the content in a div to ensure the footer works ok.
What I tried, and realized that doesn't work is:
$(document.body).wrap($('<div/>').attr('id','footerWrapper'));
The problem with this is that due to the fact that the HTML structure is generated by the user, I have been forced to inject links to the JS-file inside the <body>-tag. So now when I call wrap(), it seems that everything is first removed from $(document.body) and then appended in the new div. Since the JS-files are linked from inside , calling wrap() seems to remove them momentarily, and it seems that the scripts are unloaded by the browser and everything stops working and I'm left with a blank page. Not exactly what I had in mind.
Next idea was to first copy the JS-tags to the head-element to preserve them, so I wrapped them in a div (yeah, ugly, I know), and tried to copy them to the :
$(document.head).append($('#copyToHead').html());
That didn't do anything, and seems that $(document.head) isn't usable with functions such as .html() and .append().
So, now I'm out of ideas. Anyone have any ideas?
$(document.head) isn't usable with functions such as .html() and .append().
That would be because document.head is undefined
Use $("head")[0]
not clear on what your are trying to add to the head part. if you are simply trying to add a div to the end here is a solution:
$(document).ready(function(){
$(document.body).append($('<div></div>').attr('id','mydiv').html('This is footer'));
});
idea
If leave fact, that $(document.body) doesn't exist, wrapping everything into div and then setting id through attr might be problematic (don't ask me why—it just happens). So I played with it and created this little snippet (with preview, 100% working).
Since you can't play with html, but can "append" script I did whole document manipulation through inline script.
code
<script type="text/javascript">
$(document).ready(function(){
$("body")
.wrapInner('<div id="wrapper"/>')
.append('<div id="footer">footer text</div>');
});
</script>
preview
http://jsbin.com/ezoqo4/3
edits:
further simplification and proper markup generation
I believe this should serve you better:
$('body')
.children ().wrapAll ($('<div/>').attr('id','contentWrapper'))
.end ()
.append ($('<div/>').attr('id','footerWrapper'))
;
Ref: wrapAll

Categories

Resources