I am trying to fix some javascript code that I am currently using on a website - which was working perfectly until IE 11 came along. The script is used to first detect the type of browser used then autofills the name of a file (like a JPEG or PDF) into a specified textfield, after the file has been uploaded to the site. However I am not the original author of this script and I am very unfamiliar with Javascript. I have searched everywhere for a solution and so far have found nothing to help me with this problem. Hoping someone can help. Here is the current code:
<script langauge="javascript">
function post_value(){
BrowserDetect.init();
if(BrowserDetect.browser=='Explorer'){
var str=document.formSmallPhoto.file1.value;
var index=str.lastIndexOf("\\");
opener.document.form1.small_photo.value = str.substring(index+1);
}
BrowserDetect.init();
if(BrowserDetect.browser=='Explorer'){
var str=document.formSmallPhoto.file1.value;
var index=str.lastIndexOf("\\");
opener.document.form1.small_photo.value = str.substring(index+1);
}
if(BrowserDetect.browser=='Safari'){
var str=document.formSmallPhoto.file1.value;
var index=str.lastIndexOf("\\");
opener.document.form1.small_photo.value = str.substring(index+1);
}
if(BrowserDetect.browser=='Chrome'){
var str=document.formSmallPhoto.file1.value;
var index=str.lastIndexOf("\\");
opener.document.form1.small_photo.value = str.substring(index+1);
}
if(BrowserDetect.browser=='Opera'){
var str=document.formSmallPhoto.file1.value;
var index=str.lastIndexOf("\\");
opener.document.form1.small_photo.value = str.substring(index+1);
}
if(BrowserDetect.browser=='Firefox'){
var str=document.formSmallPhoto.file1.value;
var index=str.lastIndexOf("\\");
opener.document.form1.small_photo.value = str.substring(index+1);
}
}
function closeChildWindow(){
self.close();
}
</script>
After thinking about your comments about browser detection, I decided to play with the code a little and discovered that it's not needed and works great now. Thanks Eric Barber for your help in pointing me in the right direction! Here is the amended code:
<script langauge="javascript">
function post_value(){
{
var str=document.formSmallPhoto.file1.value;
var index=str.lastIndexOf("\\");
opener.document.form1.small_photo.value = str.substring(index+1);
}
}
function closeChildWindow(){
self.close();
}
</script>
Related
var tabs = require("sdk/tabs");
tabs.on('ready', function(tab) {
var worker = tab.attach({
contentScript:
'document.body.style.border = "5px solid red";'
});
});
by this way,seems that I can run a jscript file in a tab.But I don't know how to locate username and password like this. I notice that chrome could auto fill if I choose to remember password. How can I do this in firefox with a plugin?
I tried
function getElements(){
var x=document.getElementsByTagName("input");
console.log(x.length);
for (var i=0;i<x.length;i++){
console.log(x[i].type);
if (x[i].id=='username'){
x[i].value='haha';
}
}
}
to fill a username by contentScript,but it seems jscript does't run.And I can't find the log.Please give me a tip.Thanks:)
Finally,I find a way to inject js to current html and locate (username,password) well.
at index.js
var tabs = require("sdk/tabs");
tabs.activeTab.attach({
contentScriptFile:self.data.url("login.js")
});
at Jscript:
var x=document.getElementsByTagName("input");
var temp;
for (var i=0;i<x.length;i++){
if (x[i].type=='password'){
temp.value='haha';
x[i].value='yes';
}
temp=x[i];
}
I have the below code:
<HTML>
<HEAD>
<SCRIPT>
function myFunction(atlasTrackingURL)
{
var atlasURL = atlasTrackingURL;
if (!atlasURL) return;
//Build a cache busting mechanism
var timestamp = new Date();
var queryString = "?random=" + Math.ceil(Math.random() * 999999999999) +
timestamp.getUTCHours() + timestamp.getUTCMinutes() +
timestamp.getUTCSeconds();
//Build the final URL
atlasURL = atlasURL + queryString;
if (!document.getElementsByTagName || !document.createElement
|| !document.appendChild)
{return false;}
else
{ //Activate the JACTION call
var script = document.createElement("script");
script.type = "text/javascript";
script.src = atlasURL;
document.getElementsByTagName("head")[0].appendChild(script);
return true;
}
}
</SCRIPT>
</HEAD>
<BODY>
Test - Click Me
</BODY>
</HTML>
It works in Internet Explorer every time, but rarely works in Chrome and Firefox. Why would this be?
Can these browsers not handle a function onClick very well? Are there any other options I can take?
I am trying to help a client figure out why one of their tracking tags are not firing off all the time on click in these browsers.
Thanks,
You've got some kind of browser-dependent race condition going on, as Musa pointed out.
Try hiding the link initially, waiting for the document to load, and then adding the onclick attribute and revealing the link with javascript.
So, for example, change the link HTML to something like:
<a id="microsoft-link" href="http://www.microsoft.com" style="display: none;">Test - Click Me</a>
And add in your javascript, below the myFunction(), something like:
document.addEventListener('DOMContentLoaded', function(){
var link_elem = document.getElementById("microsoft-link");
link_elem.onclick = function() {
myFunction('http://view.atdmt.com/jaction/adoakb_PiggybackJSTest_1');
};
link_elem.style.display = 'inherit';
});
jsfiddle -- http://jsfiddle.net/m8VTy/3/
edit: I realized I may be misinterpreting what you're trying to do. What exactly is myFunction() supposed to accomplish ?
I have implemented ZeroClipboard functionality on our site - http://couponvoodoo.com/rcp/Jabong.com/coupons-offers?field_offer_type_tid=All
I am using Drupal-7
It is working fine on the desktop version but not working on the mobile version of the site.
I have put the following code in the footer :
<script type="text/javascript">
copy_coupon_footer();
function copy_coupon_footer(){
var divArray = document.getElementsByClassName("unlock_best_coupon");
for (var i = 0, len = divArray.length; i < len; ++i) {
var offer_type = divArray[i].getAttribute('data-clipboard-text');
// alert('offer_type '+offer_type );
try{
var id = divArray[i].getAttribute( 'id' );
var client = new ZeroClipboard( document.getElementById(id), {moviePath:'/moviePath' } );
client.on( 'load', function(client) {
client.on( 'complete', function(client, args) {try{
var url = this.getAttribute("href");
var coupon_code = url.split('&c=')[1].split('&')[0];
this.innerHTML = coupon_code;
var classname = this.className+' copied_coupon';
this.setAttribute("class",classname);
// window.open(url,'_blank');
window.location.href = url;
}catch(e){}
} );
} );
}catch(e){alert(e.message);}
}
return false;
}
</script>
ZeroClipboard requires Adobe Flash in order to perform it's clipboard function and thus it will not work in any browser that does not have Adobe Flash installed.
So, since there are hardly any mobile devices with Adobe Flash (only a few older Android devices), it won't work on mobile devices.
When I asked this question about an alternative to ZeroClipboard that doesn't require Adobe Flash, no other solutions were offered.
This answer may be a bit late, but I have created a pure JavaScript alternative to zeroclipboard that is very easy to use. Here it is: Github repository. Here is the code:
function clip(text) {
var copyElement = document.createElement('input');
copyElement.setAttribute('type', 'text');
copyElement.setAttribute('value', text);
copyElement = document.body.appendChild(copyElement);
copyElement.select();
document.execCommand('copy');
copyElement.remove();
}
I'm using jquery mobile to make an app. I'm playing a sound file when the page loads and displaying an image to turn the sound file off when clicked. everything is working fine however I can only get the image to change on the index page. I can turn off the sound file on any page however the image wont change on any other page. please help =) <3
<script type="text/javascript">
var newsrc = "soundOff.png";
function changeImage() {
var sample = document.getElementById("foobar");
sample.pause();
if ( newsrc == "soundOff.png" ) {
document.images["sound"].src = "/img/soundOff.png";
document.images["sound"].alt = "Sound Off";
newsrc = "soundOn.png";
}
else {
var sample = document.getElementById("foobar");
sample.play();
document.images["sound"].src = "/img/soundOn.png";
document.images["sound"].alt = "Sound On";
newsrc = "soundOff.png";
}
}
var sample = document.getElementById("foobar");
sample.play();
document.images["sound"].src = "/img/soundOff.png";
Should be this:
document.getElementById('your_id').src = "/img/soundOff.png";
Your path seems a little wierd to me.Check it properly
The code below works prefect in visual studio 2010 but doesnt work on visual studio 2008,
if there any others exception that i miss out? what else i should check for in order to know what am i missing?
Noted* i can only received alert(starting) but not alert(end1) and alert(end2)
newest edited comments - i found that my jquery file is not loaded correctly, i dont want to use url, can provide me local src? i got few project file inside a solution, my Script folder is inside one of my project file, so what should my local src look like?
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
alert("Starting");
var data = {
btnSaveDraft: "this is save draft button",
btnSubmit: "this is button submit"
}
function ShowTooltip() {
var Tip = $("<div class='dinamictip'></div>");
$("input").hover(function() {
Tip.text('');
var ToolTipID = $(this).attr('id');
var height = $(this).height() + 10;
var offset = $(this).offset();
if (data[ToolTipID].split('<br\>').length - 1) {
var temp = data[ToolTipID].split('<br\>').length - 1;
height = temp * 10;
} else {
height = 10;
}
Tip.html(data[ToolTipID]);
Tip.css('display', 'inline').css('position', 'absolute').css('background', 'lightyellow').css('border', '1px solid #cccccc').css('color', '#6c6c6c').css('left', offset.left).css('top', offset.top - height).css('border-radius', '15px')
Tip.appendTo('body');
},
function() {
Tip.remove();
});
}
$(function() {
ShowTooltip();
alert("End1");
});
alert("End2");
</script>
Use FireBug Console to check weather your script loaded or not..
add this line..instead of your jquery js file
http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js
May be this is the error, try to call in doc ready:
$(function(){
ShowTooltip();
});
Try running it in $(document).ready(function(){ }. Also, there is no need to chain multiple css() calls like that. Try using the syntax of css({prop1: 'value1', prop2: 'value2'})