I would like to write a html page where clicking on the hyperlink can be traced. That means I can get a statistic about how many people click on a hyperlink in my page, in a past period. If it is possible to know their ID, it would be better.
And I find this piece of code from the source of a web page:
<script type="text/javascript">
function stc(e,linkIndex) {
if (document.images) {
var linkText;
if (navigator.appName.toLowerCase()=="microsoft internet explorer") {
linkText=e.innerText}
else {
linkText=e.textContent}
if (linkText=="") {
if (e.firstChild) {
var firstChild=e.firstChild.nodeName.toUpperCase();
if (firstChild=="IMG") {
linkText="Image: "+getName(e.firstChild.getAttribute('src'))}}
else {
var nodeName=e.nodeName.toUpperCase();
if (nodeName=="AREA") {
linkText="ImageMap: "+e.href}}}
if (linkText=="") {
linkText=e.href}
(new Image()).src="/a/i/stg.gif?f="+escape(document.location.href)+"&t="+escape(e.href)+"&i="+linkIndex+"&n="+escape(trimString(linkText))}}
function getName(s) {
if (s.lastIndexOf('/')>=0) {
return(s.substring(s.lastIndexOf('/')+1,s.length))}
else {
return(s)}}
function trimString(s) {
return(s.replace(/^\s*/,"").replace(/\s*$/,""))}
</script>
and I guess google will be able to track information of clinking on this link.
I don't know too much about Javascript, could anyone tell me, according to this code, where the tracing information is saved?
usually trace() in other languages is is just some kind of output and the best most simple way to do that in javascript is to call console.log("some output "). you can view the output in Google Chrome by right click on the page > inspect element, then click on the console tab, there you will see your output. in Firefox, you should get the Firebug add-on, there you can see the same output generated by console.log("some output") again within the console tab.
other browsers suck for dev so why even bother explaining?
You need serverside code to do this.
For example, you could have a gateway script that redirects users to the page they want to see:
http://www.example.com/portal/www.google.com/
From there, you can just save the user's request into a database and redirect the user to www.google.com almost instantly without the user really caring.
Related
I want to save some JavaScript code as a bookmark in chrome so it automatically opens my university login site and clicks on on the login button. I am completely inexperienced in JavaScript, so I have no clue how to do this. I snipped together the following code, which opens the correct website, but then does not click on anything. The first URL automatically puts me to the login site (third URL in the code) in case I have not logged in yet in this window.
(function() {
window.location.replace("https://lms.uzh.ch/auth/MyCoursesSite/0");
window.onload = function(){
if (current_url.startswith('https://lms.uzh.ch/auth/MyCoursesSite/0')) {
return;
}
if (current_url.startsWith('https://lms.uzh.ch/dmz/')) {
document.getElementById("wayf_submit_button").click();
}
};
})();
I'm sorry if this is too obvious a question and annoys any experts but as I said I am a complete beginner. I would of course add the "javascript:" at the beginning for chrome to understand the bookmark.
When you use window.location.replace, you change the address and you code can't work anymore.
I can suggest using some browser extension, your "click function" should work then.
I guess you could also try to make some simple html page with iframe, you call your "click function" at this page, but you target it to the iframe. After that you can change browser's location to the university's webpage as you should already be logged in.
<html>
<head>
</head>
<body>
<iframe src="your_university_address.com" id="some_id" onload="click_button()"></iframe>
<script>
function click_button()
{
my_iframe=document.getElementById('some_id');
my_iframe.contentDocument.getElementById('your_button_id').click();
}
</script>
</body>
</html>
Very simple but it should do that job.
(You can achieve similar result by using window.open() I guess)
Hello StackOverflow community!
I've encountered a very weird problem and couldn't find any useful information on how to solve it.
Somehow, a piece of javascript code works only when the dev tools window is opened (docked or as a separate window) in google chrome.
The original problem: Due to our application structure, we need to open multiple popups automatically when a page is served. Since the popups are NOT opened through a direct user interaction (like onclick), modern browsers would automatically block these popups. Because of the large amount of code that would need to be refactored to avoid this, our solution was:
check if the browser is blocking some popups.
if so: inform the user about this and suggest to turn off their browser's popup blocking function for
our website (by adding it to the exception list for example).
Not a very elegant solution I know, but there was no other way so please don't comment on how to do this differently.
The javascript code:
let popupBlockingErrorShown = false;
this.OpenWindow = function (url, name, args) {
var i = Popups.length; //Popups is an array defined as a global variable that keeps track of all
//opened popup windows
Popups[i] = window.open(url, name, args);
try {
Popups[i].focus();
} catch (e) {
if (!popupBlockingErrorShown) {
alert("very user friendly message explaining to turn of popup blocking");
popupBlockingErrorShown = true;
}
};
}
The windows have to be popups. The popupBlockingErrorShown variable is to prevent having an alert message for each popup.
Works fine in firefox. But in google chrome there is this behaviour:
without dev tools open: the first popup opens normally, the others are blocked, there is no alert message.
with dev tools open: the first popup opens but gets 'stuck' on loading (it's an empty page). The alert message shows normally.
Keeping the browser-window open and simply switching between dev tools opened or closed gives the same behaviour.
Anyone can help me? Much appreciated!
This is my first stackoverflow question and I'm still very new to programming, I have a bit over a year of experience. Remarks on my 'asking questions'-skills are welcome.
Ok thanks to wOxxOm's comment I've found a workaround. So the problem was related to what window was focused on. I've added a piece of code in the catch-block to show an alert on a successfully opened popup (if there is one) :
try {
Popups[i].focus();
} catch (e) {
if (!popupBlockingErrorShown) {
if (Popups[i - 1]) { //there is a previous popup and it's been focused on.
Popups[i - 1].alert(UIMessages[33]); //show alert on opened popup.
popupBlockingErrorShown = true;
}
else {
alert(UIMessages[33]);
popupBlockingErrorShown = true;
}
}
}
Thanks #wOxxOm !
I've got what appears to be a common scenario. I have a page where someone fills out a form and rather than redirecting to a different thank you page all the tracking needs to take place on the same page.
I found this link here that explains a solution which I implemented as per the below after a successful submission (conversion ID is 11111111 and the label is 22222222).
$("#conversion-script").prop('src', '//www.googleadservices.com/pagead/conversion.js');
$("#ga-code img").prop('src', '//www.googleadservices.com/pagead/conversion/11111111/?value=0&label=22222222&guid=ON&script=0');
When this gets executed I'm monitoring the network tab for developer tools and see that the GoogleAdServices.com address gets called successfully, and not only that but I have Google Tag Assistance (Chrome extension) running and when this event fires it comes up under "Tags Found" and says "working".
When I check the adwords account however, it's not showing up. I've confirmed that the tracking codes are all correct, and the first one was made 7 days ago.
Ideas?
For example you have such button:
<a href = '#' class = 'add_to_cart'><img src = 'btn_buy.png'></a>
Try this script:
<SCRIPT src='/js/jquery.js' type=text/javascript></SCRIPT>
<SCRIPT type=text/javascript>
$(document).ready(function()
{
var add_to_cart_processed = false;
$('.add_to_cart').click(function()
{
if (!add_to_cart_processed)
{
$('body').append('<div id = "remarketing_basket" style = "display:none"></div>');
$('#remarketing_basket').load('/add_basket.txt');
}
});
});
</SCRIPT>
In file add_basket.txt you need to save your conversion code.
i am developing a website, i want disable print screen so i searched in the net and i used a JavaScript to disable print screen. it worked fine but during loading of a page it asks for the permission to access the clipboard.
the pop-up message that it shows,
"do u want to allow this webpage to access your clipboard?
If you allow this, the webpage can access the clipboard and read information that you've cut or copied recently.. "
i want to avoid the above pop-up message and also disable print screen.
Below is my JavaScript code.:
function AccessClipboardData() {
try {
window.clipboardData.setData('text', "Print Disabled");
} catch (err) {
}
<body>
<script language="JavaScript" type="text/javascript">
setInterval("AccessClipboardData()", 300);
var ClipBoardText = "";
if (window.clipboardData) {
ClipBoardText = window.clipboardData.getData('text');
ClipBoardText = window.clipboardData.clearData('text');
if (ClipBoardText == "") {
alert('Sorry you have to allow the page to access clipboard');
document.all("divmaster").style.display = "none"
}
}
</script>
</body>
can please help to solve that print screen and clip board problem.
thanks in advance..
You must be trying to protect your page from theft or copy.
But I have only one to say to you. If the website loads on the clients computers, it means all the contents including images to markup all of them are stored on the client's PC and then displayed on the browser window. So, not matter what you do, there is never a final solution for this.
So, I suggest you to do not go down this road.
<input name="Print1" onclick="javascript:window.print();" type="button"
value="Print1" align="right"/> use this
onclick="javascript:window.print();" type="button" value="Print1" align="right"
I have been to some css/html/js discussing board which provide a text box to enter the html and a "Run it!" button to run the html in new pops up window.
I want to make one also, which is easy in jQuery:
function try_show_result() {
var code = $("#try-input").val();
if (code !== "") {
var newwin = window.open('','','');
newwin.opener = null; // 防æ¢ä»£ç 修改主页
newwin.document.write(code);
newwin.document.close();
}
}
But then I found a security problem: the pops up window has all the abilities of running an arbitrary javascript. So that when another authenticated user runs a given piece of code on the page, then it could stealing cookies or access some url that is only for the specified user only through ajax posts.
Is there an easy way to avoid this?
Update: I added newwin.document.cookie="" before open the window, not sure if this is better.
Is there an easy way to avoid this?
No
That is why Facebook went out and wrote their own version of JavaScript [FBJS].