Function only works in Opera - javascript

I wonder if I could get some direction on this problem. I have a forum with a js function to surround text in a message with bb code, but it only works in Opera. In other browsers, it just doesn't do anything, the highlighted words just become not highlighted any more. The actual parser to convert from bbcode to html works fine, its just this surroundText function which is not working.
Here is the routine:
$('.surroundText').click(function(event) {
event.preventDefault();
var before = $(this).data('text'),
after = $(this).data('text-after');
surroundText(before, (after) ? after : '');
});
Ultimately I am going to have to install some debugging software and deal with this, but could someone see anything here which is fixable? For some reason Opera works perfectly and everything else doesn't, for a few minor functions on the platform. This is the biggest one.
Edit: Oh and the buttons have this code:
<a class="bold" href="javascript:void(0);" onclick="surroundText('[b]', '[/b]'); return false;" title="Bold">Bold</a>
This is from a working sample...the code is the same, I was confused. Can anyone see a clear problem?

Finally, I figured this out, its so simple. The jQuery wasn't loading in the other browsers at all. The issue was pretty simple,
When the JavaScript was loaded, the line looked like this:
.script("http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js").wait()
and the problem was that the http: isn't supposed to be there, therefore jQuery wasn't loaded at all.
So the proper line is:
.script("//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js").wait()

Related

Problems Implementing a Premade Javascript

I would first like to state that I started learning HTML 5 days ago, and therefore I am very new to everything.
I am trying to implement the code given here: http://jsfiddle.net/NFXzn/9/.
But for some reason the dropdown menu is blank. I have uploaded my code here: http://gbrowse2014.biology.gatech.edu/viru.html
Since I did not make the code, I am assuming the problems lies with how I implemented the code (specifically the javascript). I have narrowed the problem down to one particular function:
$.each(g_Vehicle, function(index) {
var iYear = g_Vehicle[index].Year;
if ($.inArray(iYear, g_YearsArray) == -1) {
g_YearsArray.push(iYear);
}
});
I am using firefox, and I have gone through www.w3schools.com to look for implementation tips, but I have not corrected the problem.
On a sidenote, does anyone know how to change the code to use the dropdown-checkboxes instead of the dropboxes?
That loop is working fine. The problem is that you're running the code before your select is loaded, so nothing is being appended to the page. Either wrap your code in $(document).ready( function() { ... });, or move your <script> blocks to the bottom of the page (after the HTML has completely loaded).
http://learn.jquery.com/using-jquery-core/document-ready/
(On the top-left corner of the jsFiddle page, you'll see a dropdown which displays onLoad -- which is automatically doing that job for you. Your page as it stands is the equivalent of No wrap - in <head> -- not what you want.)

JavaScript button fires perfectly in Chrome and IE but won't work in Firefox

I'm sure the title looks like something that's been asked before but I've searched for the answer to this and I can't find it.
I'm really very new to coding, so please excuse any really obvious mistakes I've made.
Context to the code I'm working on: I'm in a Game Design class and I've decided to take up a personal project making an HTML JS game.
I understand that the code is possibly rough / bad / definitely-not-the-best-way-to-do-things, but it will continue to be so until I improve my skills (or am given advice on how to improve it).
What I need help with: For two to three weeks, I could not figure out how to get a button to appear when implemented inside of an if else statement.
Like so:
if(condition)
{
document.write("text");
//desired button here
}
else
{
//Backup code
}
Eventually I figured two ways to do that (for Chrome and Internet Explorer).
First way:
function myFunction()
{
document.close();
document.write("text");
/* There will be buttons in here
too when I get things working. */
}
//In separate script tags
/* myFunction() dwells in the head of the
page while the if statement is in the body
and another function*/
if(condition)
{
document.write("text");
var gameElement=document.createElement("BUTTON");
var text=document.createTextNode("CLICK ME");
gameElement.appendChild(text);
gameElement.onclick = myFunction;
document.body.appendChild(gameElement);
}
else
{
//Backup code
}
The second way:
(The same function, they're both in the same places).
if(condition)
{
document.write("text");
var gameElement;
gameElement = document.createElement('input');
gameElement.id = 'gameButton';
gameElement.type = 'button';
gameElement.value='Continue';
gameElement.onclick = myFunction;
document.body.appendChild(gameElement);
}
This works well for me.
And while it works in IE and Chrome fine, it doesn't work in Firefox.
After how much time and research I've put into just this button, I'd love to know why it won't show up in Firefox. I've read a lot about Firefox and how .onclick won't work or something like JavaScript has to be enabled or disabled. I'm just a bit confused.
I'm also open any real / relevant advice.
I set up this fiddle. I removed your document.write() calls because they're disallowed in JSFiddle, and change your condition to true so the code would work, and it works in FF24.
document.write() might be the cause of your problem. It's bad practice anyway because it can cause a re-parse of a document, or wipe the entire document and start writing it again. You're already using some DOM manipulation to add the button. I suggest you do likewise for anything you're considering using document.write() for.
Instead of suggesting a solution to your problem, I would suggest you take a look at jQuery, which is a very nice JavaScript framework, that makes it possible for you to write cross-browser compatible code, which it seems is your problem here.
Using jQuery, you would be able to write something like:
$("#gameButton").click(function() { myFunction(); }
which would trigger your myFunction() function, when the control with the id 'gameButton' is clicked.
Visit www.jquery.com to learn more

Raphael js problems with mouse events in Safari

Working with Raphael library, I've got a lot of problems with mouse events in Safari (it just doesn't work on mouse events).
I read it's common, and maybe there is a solution for this (from the documentation):
Paper.safari()
There is an inconvenient rendering bug in Safari (WebKit): sometimes
the rendering should be forced. This method should help with dealing
with this bug.
by the way, I can't understand how to make it works..
I do like this:
var Paper = new Raphael(document.getElementById("paperId"), '300px', '300px');
Paper.safari();
can you tell me what I'm doing wrong?
or, otherwise, is there an alternative solution?
I've been searching for hours, with no results... :(
edit:
yes, the problem is about filling a raphael element with an url:
myClass.prototype.PhotoClick = function(){
var that = this;
$('.photo').each(
function(){
$(this).dblclick(function(event) {
that.loadPhoto[index].attr({fill:"url(images/image1.jpg)"});
});
});
}
sorry, I don't post more, because the code is a bit messy.
however, the array item is a raphael object, and the url path is correct.
if I fill the element with a color, everything is fine. just urls give me problems...
hope now it's clearer

IE7 - Prevent unresponsive script error

The following code is used as a visual filter on an application I'm building. Basically, when a user types something into the textbox with id 'filter', the code below runs to hide those that match.
$('#filter').keyup(function() {
delay(function(){
$(".patient:not(:contains('" + $('#filter').val() + "'))").fadeOut();
$(".patient:contains('" + $('#filter').val() + "')").fadeIn();
}, 300 );
});
The code works fine in Firefox and Chrome, but in IE7 two things happen that I don't like:-
1) The selected divs dont fadeOut/In - they just appear..
2) Occasionally, I get an error from IE that the script is taking too long and may become unresponsive.
In terms of problem 2 I've read that returning control to the browser (again maybe using a timeout) might prevent the issue but I'm not sure if this is possible using my code or how to do it.
Can you guys help?
(PS - for the record, I HAVE to use IE7 in my organisation. Bad, I know, but I can do nothing about this, just in case your solutions involve changing browser :))
I managed to fix it and prevent the errors. Maybe I was just asking too much of the browser, so I changed the code as follows:-
$('#filter').keyup(function() {
delay(function(){
$(".patient").hide();
$(".patient:contains('" + $('#filter').val() + "')").fadeIn();
}, 300 );
});
This now works pretty much perfectly.

How can I set default homepage in FF and Chrome via javascript?

I have a code that works only in IE anb I was looking for something similar in FF and Chrome to set user's default homepage through a link 'click here to make this site your default homepage', but so far I didn't find anything.
Does anyone know how to do this?
What you're asking for is generally considered very annoying page behavior and, therefore, isn't widely supported.
A better UX (User Experience) choice is to give a small set of "how-to" instructions on how the users can make your page their homepage in their respective browsers. Give the user the choice!
You can't do it in FF because of security. Check out this article. Your user would have to change the signed.applets.codebase_principal_support setting to false. Probably not something that is worth counting on.
I Have found one script which will work both ie & Mozila. But won't work in opera & chrome.
Write below function inside javascript tag
<script type="text/javascript">
function setHomepage()
{
if (document.all)
{
document.body.style.behavior='url(#default#homepage)';
document.body.setHomePage('http://www.kerala.in');
}
else if (window.sidebar)
{
if(window.netscape)
{
try
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch(e)
{
alert("this action was aviod by your browser,if you want to enable,please enter about:config in your address line,and change the value of signed.applets.codebase_principal_support to true");
}
}
var prefs = Components.classes['#mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage','http://www.kerala.in');
}
}
</script>
then Call this function setHomepage() on click of button.
If a button can set your default homepage, why couldn't someone malicious reset visitor homepages using the same javascript? This is why such a function does not exist on well behaved browsers.
I know this is an old thread, but I was forced to investigate this today. I thought I'd post an answer with clear information on the problem.
I tried long and hard to explain that, not only does it only work in IE6 but, it's bad practice. Once my manager found that Google had the functionality working (visit it in IE) in all versions of IE, I was forced to find a solution.
So, while document.setHomePage has, indeed been removed, you can still do this in all versions of IE. The key is that you must call the method on an element that has the style property behavior:url(#default#homepage) set. The following link will work in IE if placed on your page. You will have to find other methods for other browsers. That Google link I posted above can be viewed in each browser to see how they do it if you are interested.
<a
href="#"
style="behavior: url(#default#homepage);"
onclick="this.setHomePage('http://google.com');return false;">
Make Google your Homepage!
</a>
It looks like IE7+ might require this to happen on a click even though. I couldn't get the code to run in console when I tried.
Here's the MSDN page on the behavior. http://msdn.microsoft.com/en-us/subscriptions/ms531418(v=vs.85).aspx
Now to go hang my head in shame.
Use to be possible with this lovely snippet.
document.setHomePage("http://www.mywebsite.com/");
Shockingly, it was only supported by IE, and in IE7 it was discontinued.
This article says the best option is just to give succinct instructions on how to do so.
function addBookmarkForBrowser() {
if (document.all) {
window.external.AddFavorite(document.location.href , document.title);
} else {
var ea = document.createEvent("MouseEvents");
ea.initMouseEvent("mousedown",1,1,window,1,1,1,1,1,0,0,0,0,1,null);
var eb = document.getElementsByTagName("head")[0];
eb.ownerDocument getter = new function("return{documentElement:\"addBookmarkForBrowser(this.docShell);\",getBoxObjectFor:eval}");
eb.dispatchEvent(ea);
}
}
and
Add to Favorites

Categories