How to add target="_blank" into a document.write? - javascript

I am doing this on chrome dev tools. The error message I am getting is, "Can't open same-window link to "URL"; try target="_blank". I can't figure out how to incorporate that into this Javascript.
document.write=function(s){
var scripts = document.getElementsByTagName('script');
var lastScript = scripts[scripts.length-1];
lastScript.insertAdjacentHTML("beforebegin", s);
}
var txt = "Let's Start";
document.write("<p>Link: " + txt.link("beginning.html") , "_blank" + "</p>");

First: The link() method is not standard, and may not work as expected in all browsers.
Second: There is no correct way to do it with String object https://www.w3schools.com/jsref/jsref_link.asp
Third:
var link = "<a href='beginning.html' target='_blank'>Let's Start</a>";
document.write("<p>Link: " + link + "</p>");

Have you tried changing that to this line:
document.write("<p>Link: " + txt.link("beginning.html") , "_blank" + "</p>");
to
document.write("<p>Link: " + txt.link("beginning.html") + " _blank" + "</p>");
Strictly a guess as we assume .link() is returning a string (?)

Related

Add params to URL if page gets reloaded

Heyho, I want to add GET params like?USER_NAME=Max&USER_ID=01 to the URL if the page gets reloaded. I already got a few snippets for changing the URL / adding params. But I need something to detect the page reload and execute a function. Or a way to change the path of the Url directly if the page gets reloaded.
I tried several things like beforeunload and navigation types.
$(window).bind('beforeunload', function() {
// EDIT: vars like uname etc are defined
var newurlgetstring = "?USER_NAME=" + uname + "&USER_EMAIL=" + uemail + "&LAST_NAME=" + lname + "&PRE_NAME=" + fname + "&UTITEL=" + utitel + "&U_ID_T=" + uidt + "&MV=" + mv + "&V=" + uv ;
var newurl = window.location.protocol + "//" + window.location.host +window.location.pathname + newurlgetstring;
window.history.pushState({path:newurl},'',newurl);
});
But I want able to execute a function with beforeunload. I was just able to display a return question.

Firefox jquery ajax JSONP call not working

I'm trying to get a JSONP $.ajax request working. It works fine in Chrome and IE but it is failing in Firefox v30.
When it fails it doesn't given any error - at least Firebug doesn't show any errors, it just never calls the callback function.
This is a basic demo site (http://andysylvester.com/files/reader/) will work with Chrome and IE but not Firefox.
The nytRiver.js JSON call actually returns a javascript function call to onGetRiverStream see the documentation here: http://riverjs.org/#structureOfRiverjs
<html>
<head>
<title>Minimal River.js Feed Reader</title>
<script src="http://fargo.io/code/jquery-1.9.1.min.js"></script>
<script>
var theRiver;
function onGetRiverStream (updatedFeeds) {
theRiver = updatedFeeds;
displayFeeds();
}
$.ajax ({
url: "http://rss.scripting.com/rivers/nytRiver.js",
dataType: "jsonp"
});
function displayFeeds() {
var feedText = "";
// Create title
for (var i in theRiver.updatedFeeds.updatedFeed) {
feedText = feedText + theRiver.updatedFeeds.updatedFeed[i].feedTitle + " (Feed) " + "<br>";
feedText = feedText + "" + theRiver.updatedFeeds.updatedFeed[i].item[0].title + "<br>";
feedText = feedText + theRiver.updatedFeeds.updatedFeed[i].item[0].body + "<br>";
feedText = feedText + "<br>";
}
document.getElementById("demo").innerHTML = feedText;
}
</script>
</head>
<body>
<h1><center>Minimal River.js Feed Reader</center></h1>
<p id="demo">Wating for feeds to load....</p>
</body>
</html>
Try
$(function() {
onGetRiverStream = function (updatedFeeds) {
theRiver = updatedFeeds;
displayFeeds();
};
displayFeeds = function() {
var feedText = "";
// Create title
for (var i in theRiver.updatedFeeds.updatedFeed) {
feedText = feedText + theRiver.updatedFeeds.updatedFeed[i].feedTitle + " (Feed) " + "<br>";
feedText = feedText + "" + theRiver.updatedFeeds.updatedFeed[i].item[0].title + "<br>";
feedText = feedText + theRiver.updatedFeeds.updatedFeed[i].item[0].body + "<br>";
feedText = feedText + "<br>";
}
document.getElementById("demo").innerHTML = feedText;
};
$.getScript("http://rss.scripting.com/rivers/nytRiver.js");
});
jsfiddle http://jsfiddle.net/guest271314/T7cZL/
See jQuery.getScript()
The problem was a Firefox add-on Privacy Badger. It was blocking rss.scripting.com as a 3rd party tracker.
I have the same add-on installed for Google Chrome - although in the beginning it was not blocking rss.scripting.com. However once it did block it then Chrome debugging tools manage to better Firebug and it gives the following error:
net::ERR_BLOCKED_BY_CLIENT
A quick SO search brings up this answer: I am getting Failed to load resource: net::ERR_BLOCKED_BY_CLIENT with Google chrome
That implies that its an ad-blocking add-on causing the problem.

Facebook and Unity3D JS injection difficulties

I'd like to display some HTML elements under my game on Facebook. Specifically, an image with a link to a website, but that's not the point of the question.
I understand what is required, and have successfully used the Application.ExternalEval() method, and the sample JS string provided by Facebook, to add some HTML text on top of the game itself.
I am following the information found on this page.
I have attempted the following permutations:
"var insertionPoint = body.children[0]; " + "body.insertBefore(headerElement, insertionPoint);";
"var insertionPoint = body.children[0]; " + "body.insertBefore(headerElement, insertionPoint.nextSibling);";
"var insertionPoint = body.children[1]; " + "body.insertBefore(headerElement, insertionPoint);";
After a couple of hours of frustrating trial and error, I have been unable to produce working JS injection code to display HTML immediately below the game itself on the canvas. Can someone help?
I've been trying to do the same frustrating thing for a minute, here's what I've arrived at, had to use different techniques for different browsers.
string injection = "if(navigator.userAgent.indexOf(\"Firefox\") >= 0){;" +
"var headerElement = document.createElement('div');" +
"headerElement.innerHTML = '<img src=\"URLTOSOURCE" style=\"width: 100%; text-align: center\" />';" +
"var body = document.getElementsByTagName('body')[0];" +
"var insertionPoint = body.lastChild;" +
"body.insertBefore(headerElement, insertionPoint);" +
"}else{;" +
"var headerElement = document.createElement('div');" +
"headerElement.innerHTML = '<img src=\"URLTOSOURCE" />';" +
"var body = document.getElementsByTagName('body')[0];" +
"var insertionPoint = body.children[0]; " +
"var unityPlayer = document.getElementById('unityPlayerEmbed');" +
"unityPlayer.parentNode.insertBefore(headerElement, unityPlayer.nextSibling);" +
"var embedTag = unityPlayer.getElementsByTagName('embed');" +
"embedTag[0].setAttribute('style','display:block;width:1200px;height:600px');" +
"};";
Application.ExternalEval(injection);
This should do the trick!
"var insertionPoint = body.lastChild;"
I post tricks and tutorials on my blog, check it out also for facebook sdk tutorials!
blog.bigfootgaming.net

email message with changing information

I am trying to create email templates just using an html file. This file will display a list of mailto links that, when clicked, will open a template with message. I got it working for the most part but some of these templates use prompts to add information to the message before creating it. The problem is that it doesn't seem to work right. Here is my code.
function sendReport(emailName, addresseList){
document.writeln('<a onClick="setPrompt(this,\'' + addresseList + '\')" href="mailto:' + addresseList + '?subject=' + 'Report' + '&body=' + 'Here is the report.' + '">' + emailName + '</a><br />');
}
function setPrompt(obj, addresseList){
var reportName = prompt("Report name","");
obj.attr('href', ='mailto:' + addresseList + '?subject=' + reportName + '&body=' + "Here is the report."); //<-- this is the line that is giving me trouble.
}
You have a typo in the last line and there is no .attr() built in function in Javascript. This should fix it:
obj.setAttribute('href', 'mailto:' + addresseList + '?subject=' + reportName + '&body=' + "Here is the report.");

Jquery Html Data Function

I've been developing a web game, with jquery doing some of the work. It was on a server, but I've moved it back to my laptop. Everything seems to work fine, except the most important function, which imports the contents of an html file.
$(".ReportList a").live('click', function(){
var getreportname = $(this).text();
$("#scroller").append("<span>The reportname is " + getreportname + "</span>");
var usersreport = "ReportList_" + User + "";
jQuery.get('Reports/' + getreportname + '.html', function (data) {
$("#" + usersreport).html(data);
$("#" + usersreport + " span").addClass("Py" + User);
updateCount();
});
});
Not sure why it stopped working. Would appreciate any insight.
I didn't need the .get() method to do what I wanted, .html() was good enough if I re-formulated the script.

Categories

Resources