google analytics Tracking code not firing - google-analytics

I have this tracking code, I am not sure what else I can do to it to make it fire. When the page loads nothing happens, no errors, I just don't see the data being sent to google.
<script>
if (hotelname == " foohotels")
{
document.write(<img height="1" width="1" border="0" alt="" src="https://www.googlefoo.com/xxxx/clk/pagead/conversion/xxxx/?label=HPA&guid=ON&script=0&ord=$$random$$&data=xxx_partner_hotel_id%3Dhotel-name%3Bhct_base_price%3Dtotal%3Bhct_total_price%3Dtotal2%3Bhct_currency_code%3Dcurrency%3Bhct_checkin_date%3Darrival%3Bhct_length_of_stay%3Dnights%3Bhct_date_format%3D%Y-%m-%d%3Bhct_booking_xref%3Dbooking%3Bhct_ver%3D1.0.i"/>);
}
</script>

Two things:
Syntax: document.write() expects a string, something between single or double quotes (good thing that the string can contain the other type). As your thing contains double quotes already, I suggest surrounding it with single quotes:
document.write('<img height="1"...');
What it does: document.write() overwrites the document, see this example:
function test(){
document.write("I am brand new content, everything else is gone.");
}
<button onclick="test()">Clicky</button>
So it is not sure that such change (having a proper string) will cover everything you need, you might better use document.createElement("img"), set its attributes, and put it somewhere in the existing document.

Related

Using an <embed> tag with Meteor.js

I have a group of pdfs on another domain, and their urls in my Meteor app. My intention is to click a button, have it set a session variable and new data context, and have the associated pdf show up as an embedded object.
What's happening, is that if I directly reference it like below, it shows up the first time, but it doesn't update when I change data contexts.
<embed src="{{selectedSheet.docPath}}" type="application/pdf">
I've also tried embedding it all within a {{#with}} block, but in this case nothing will show up. It's definitely the <embed> that's breaking it -- if I remove that, this block works as expected.
{{#with selectedSheet}}
<embed src="{{docPath}}" type="application/pdf">
{{/with}}
I'm using this helper to get my selectedSheet, and I've verified that it's not an issue with the Session.get portion of it.
Template.bldgLevel.helpers({
selectedSheet: function(){
return Documents.findOne({"_id": Session.get("currentSheet")});
}
});
Any help appreciated. I'm pretty new at this :)

one button to refresh multiple iframes on iframe name

Dear stackoverflow users,
I have the following code:
<button onclick="var ifr=document.getElementsByName('menuframe')[0]; ifr.src=ifr.src;">Refresh Iframes</button>
And this code only changes the first iframe. I have tried changing the [0] to [1] and this lets me refresh the second iframe but not the first.
So i tried [0-10] but that did not do anything. Also i tried [0,1] but this changed the second.
I know it must be possible but how. I have no idea. Any help would be greatly appreciated!
I know this is an old question, but I figured I'd throw an answer.
To start I would recommend calling a function in the onclick attribute instead of including inline javascript. You would accomplish this using the following:
<button onclick="javascript:refreshIframes('menuFrame');">Refresh Iframes</button>
I added a variable to the function to allow it to be reusable for other iframe names.
In your page (most people prefer to place it at the bottom, some prefer to place it in an external javascript file that is then referenced) you would include a function similar to the following:
function refreshIframes(frameName) {
var frames = document.getElementsByName(frameName);
for (var i = 0; i < frames.length; i++) {
var frame = frames[i];
frame.src = frame.src;
}
}
If you decide to include this on your page instead of putting it inside an external javascript file you would need to wrap it in script tags.

Unobtrusive JavaScript with server side values, best practice?

I'm used to do doing things like this:
<a href="Javascript:void(0);" onclick="GetCommentForGeneralObservation(<%# Eval("ID") %>)">
That would be in a repeater or such like btw. However I an now trying to use unobtrusive JavaScript and hence get rid of any JS in the markup. I was trying to figure out what the best practice is in cases like this? I've used attributes and then got the value using JQuery to pass to the AJAX call but it seems a bit of a hack.
Edit in light of first reply:
I was thinking more of the
Separation of functionality (the "behavior layer") from a Web page's structure/content and presentation.
part of unobtrusive JS as I understand it.
This happens to be for an application where I don't have to worry about Javascript being turned off on the client, it's hosted internally at my company. What I was getting at was how would I get the value from Eval("ID") into the JS call if I were to attach the onclick event in a separate .js file via JQuery.
Sorry for not being clearer. I appreciate the need for progressive enhancement in public facing apps.
In HTML 5 you are allowed to define your own attributes prefixed with 'data-'
e.g.
<a
class="comment"
data-rendered-id="<%# Eval("ID") %>"
href="/getCommentForGeneralObservation/<%# Eval("ID") %>" >
And then use that attribute in jQuery in your click event handler.
$(".comment").click(function () {
var id = $(this).attr("data-rendered-id");
return GetCommentForGeneralObservation(id);
});
This will work in most pre-HTML5 browsers too as long as they support jQuery.
Note that in unobtrusive javascript you really should support non-javascript browsers, hence you need an href attribute that works as well.
I'd start with:
<a href="/getCommentForGeneralObservation/<%# Eval("ID") %>" class="getCommentForGeneralObservation">
and then attach an event handler that looked something like this:
function (event) {
var href = this.href;
var id = href.search(/\/(\d+)/);
return GetCommentForGeneralObservation(id);
};
Unobtrusive means you don't depend on Javascript for functionality and therefore your code is extended with Javascript rather than replaced by it.
In your case, you're embedding Javascript directly in the link, and worse off, the link won't work at all without Javascript enabled. Instead you'll want something like this, which is pure HTML without any reference to Javascript:
<a id="commentLink" href="comment.asp">Get Comment</a>
And your Javascript (assuming you're not using a framework) would be something like:
function GetCommentForGeneralObservation(evt) {
// Extra javascript functionality here
}
document.getElementById("commentLink").onclick = GetCommentForGeneralObservation;
With Jquery I believe you could just do:
$("#commentLink").click(GetCommentForGeneralObservation);
I'm going to re-answer this because I understand the question now and the approach I usually use is different from what has been suggested so far.
When there's no way to avoid having dynamic content, I will usually do the following:
<script type="text/javascript">
var myApp = {commentId:<%# Eval("ID") %>};
</script>
<script type="text/javascript" src="myAppScript.js"></script>
Now, in myAppScript.js, you can use myApp["commentId"] wherever you need that ID to be referenced. (I use a dictionary so as to not pollute the global namespace)
The advantage of this approach is that your myAppScript.js is still completely static and so you can serve it very fast. It also has the advantage of keeping the relevant information out of the URL and you can use Javascript objects, which can help a lot with complex and/or hard-to-parse data.
The disadvantage is that it requires inline Javascript, which isn't much of a disadvantage unless you're a web perfectionist.
I also really like DanSingerman's approach, which is more suited if your data is specific to a tag.
You might wish to use JQuery metadata plugin, or the core data function.
http://docs.jquery.com/Core/data
http://plugins.jquery.com/project/metadata
You could use the rel attribute of the anchor tag to store the value you want to associate with the link.
Click Here
Then in your jQuery code you can check the rel attribute to get the value you're looking for.
$(".comment").click(function () {
return GetCommentForGeneralObservation(this.rel);
});
(Pretty sure this is accurate for jQuery, but I'm more of a MooTools guy...)
How about making sure it is fully unobtrusive?
In the head of the HTML document
<script src="path/to/php/or/other/equivalent/server/side/file"></script>
In the path/to/php/or/other/equivalent/server/side/file:
var SiteServerVariablesEtc = {
someProperty: <?php echo "hello" ?>
}
In your normal JS file:
var myVar = SiteServerVariablesEtc.someProperty;

Facebox adding commas to input

I'm using a facebox to display a form inside a lightbox, nothing too exciting (just a couple of datepickers, some textboxes and a checkbox).
However, I'm having issues with the postbacks, whenever I post back from the facebox it adds a ',' to the start of the input (so "rabbit" becomes ",rabbit")
Now, I saw that there was the same issue with the modalpopup extender from the ajaxcontroltoolkit, so I assume it's a common issue.
Can anyone either explain why this is happening, or tell me how to fix it? provide a decent way of fixing this? I have actually done it, and it works very nicely, but I don't really want to answer my own bounty question so someone else give it a go!
Cheers, Ed
EDIT
See attached answer for a correct solution (I fixed this eventually but didn't want to ruin the bounty question so left the answer until afterwards).
Why don't you trim the output? simply remove the ',' for each string
I have never programmed in ASP.NET or used facebox for that matter of fact, but here's a couple of solutions from my little research that might work.
There is a reveal function in the facebox source where the actual cloning is done:
reveal: function(data, klass) {
$(document).trigger('beforeReveal.facebox')
if (klass) $('#facebox .content').addClass(klass)
$('#facebox .content').append(data) // <--- This does the cloning
The extra comma is obviously from the original form field that has been repeated. You could bind a click() function to the submit button that submits the form and in that function remove one of the clones. Since this function should run before the form data is processed, the duplicates should be taken care of.
$("#my-submit-button").click(function() { $('#facebox .content').empty(); }
If that doesn't work, then this surely will. Facebox has a bunch of hooks to run your code after various events. One of the hooks is reveal.facebox or afterReveal.facebox. Since the cloning is done on reveal, you would have to bind a custom function to run at this event, and in that function change the id/names of all elements. Append a random word like _temp or something for each element. Not the exact code, but I hope you get the idea.
(document).bind('reveal.facebox', function() {
$("#facebox .content > *").each(
// change the id's/name's
);
});
Edit:
Looking at the html for Facebox examples, it looks like it lives inside its own <div> and copies anything that has to be shown within that div. So the structure of a sample facebox page could look like:
<html>
..
<form runat="server">
<div id="myForm">
// original form controls go here, probably hidden
<input id="theId" type="text" value="" />
</div>
<div id="facebox">
...
<div class="content">
// the original form is copied inside this space and then displayed
// this is the one the user interacts with and makes changes to
<input id="theId" type="text" value="new value" />
</div>
...
</div>
</form>
..
</html>
So based on this structure and example, the input box with id=theId appears inside div#myForm and div#facebox. div#facebox is the one with the updated values that we need.
Ok, Here's how I fixed it:
changing
function fillFaceboxFromHref(href, klass) {
....
if (href.match(/#/)) {
var url = window.location.href.split('#')[0]
var target = href.replace(url, '')
$.facebox.reveal($(target).clone().show(), klass)
....
}
to
function fillFaceboxFromHref(href, klass) {
....
if (href.match(/#/)) {
var url = window.location.href.split('#')[0]
var target = href.replace(url, '')
$.facebox.reveal($(target).show(), klass)
....
}
will stop it cloning the input and instead use the actual div.
Then, it's simply a case of re-appending the inner content to the #aspnetform (or #body as it originally used [you have to change that to allow asp.net postbacks]) before it's cleared in the close.facebox binding, like so:
$(document).bind('close.facebox', function() {
/// two added lines to add the content back to the #aspnetForm, with display:none; (i.e. invisible)
$('#facebox .content').children().css({'display' : 'none'});
$('#aspnetForm').append($('#facebox .content').html());
/// extra line to make sure there's no flashing effect as the facebox closes: the content is still in there too!
$('#facebox .content').children().css({ 'display': 'block' });
....
This will now use the original div as the content, avoiding the comma problem. However, if you wanted to use a div that is visible on the page originally then some extra twiddling would be needed in the close.facebox binding.

Encoded character is used instead the correct one

I have a little problem and I'm hopping that you can help me solve this annoying issue.
I need to use an iFrame in an administration panel to let users use the selection service, and in the HTML I have:
<iframe scrolling="yes" runat="server" title="Par Selection" id="iFrame"
frameborder="0" enableviewstate="true" width="100%" height="490" />
in my code-behind file I have:
iFrame.Attributes.Add("src", String.Format(
"https://www.parurval.se/urval/?username={0}&password={1}",
parSettings.GetSettings(parSettings.SettingsType.PARSelection, parSettings.SectionType.Username),
parSettings.GetSettings(parSettings.SettingsType.PARSelection, parSettings.SectionType.Password)));
The output is this:
<iframe id="tcMain_tabPARSelection_iFrame" scrolling="yes" title="Par Selection"
frameborder="0" width="100%" height="490"
src="https://www.parurval.se/urval/?username=myUsername&password=myPassword">
</iframe>
Please note the & instead & sign in the src address when passing username and password
How can I prevent this?
I tried with HttpUtility.Decode( myCompleteUrl ) but with the same achievement :(
The worst thing is, if the src code has only the address
... src="https://www.parurval.se/urval/" ...
I'm not able to input the user/pwd, I see the form and I can enter text, but it does nothing, it only refreshes the iframe inner page, doing this in a full window, works fine.
And in that administration panel I have a textbox to the user add the username and password in order that entering the Administration page, I will jump directly to the service in the iFrame so the user does not need to enter user/pwd to login every time, that is way I'm trying to add those values dynamically.
Any ideas?
Added:
If I put the correct URL address (with user and pwd) in the iFrame src attribute in the HTML side (not dynamically) all works fine :(
The presense of the & is actually correct there. Most browsers are forgiving enough not to choke on just seeing & there, but it's technically not correct.
“&” is a special character in HTML (more specifically in SGML), so encoding it is the correct thing to do. Yes, even in link URLs.
The HTML 4.01 specification states:
Authors should use "&" (ASCII decimal 38) instead of "&" to avoid confusion with the beginning of a character reference (entity reference open delimiter). Authors should also use "&" in attribute values since character references are allowed within CDATA attribute values.
So encoding the & as & is correct behavior since the interpretation of the src attribute value (CDATA data type) is described as:
CDATA is a sequence of characters from the document character set and may include character entities. User agents should interpret attribute values as follows:
Replace character entities with characters,
Ignore line feeds,
Replace each carriage return or tab with a single space.
Otherwise src attribute values like /foo?bar&sect=123 would be ambiguous as they can be interpreted either literally as /foo?bar&sect=123 or (replacing the sect entity) as /foo?bar§=123.
This seems like a case where you can take advantage of URL encoding to hide the &, bypassing XML encoding. & is U+0025, so you can encode it as %25: https://www.parurval.se/urval/?username={0}%25password={1}
You should use
HttpUtility.HtmlEncode(String.Format("https://www.parurval.se/urval/?username={0}&password={1}",
parSettings.GetSettings(parSettings.SettingsType.PARSelection, parSettings.SectionType.Username),
parSettings.GetSettings(parSettings.SettingsType.PARSelection, parSettings.SectionType.Password)));

Resources