execcommand not working in opera - opera

i have this code, execcommand for BOLD, and it is not working under opera. anybody have some idea why ? thanks
<script type="text/javascript">
function SetToBold () {
Editor.execCommand ('bold', false, null);
}
function start() {
frames.iView.document.designMode = "On";
var myIFrame = document.getElementById("iView");
browser = navigator.appName;
if (document.all) { //IE
var iframe_window = window.frames["iView"];
iframe_window.document.body.focus();
}
else { //Firefox
field = myIFrame.contentWindow.document.getElementsByTagName("body")[0];
setTimeout("field.focus()",250);
}
if (browser == "Netscape") {
Editor = myIFrame.contentWindow.document;
Editor.designMode = 'On';
Editor.body.contentEditable = 'true';
}
else if(browser == "Microsoft Internet Explorer") {
frames['iView'].document.designMode='On';
Editor = frames['iView'].document;
}
}
i call start function with body onload. it is ok firefox and ie, but doesnt work in opera.
anybody knows about some restrictions of execcommand in opera ? thanks

In the quoted script, the variable "Editor" is given a value only inside the browser sniffing section. In Opera "Editor" will be undefined and hence trying to do "Editor.execCommand()" will throw an error.
Rather than
if (browser == "Netscape") {
Editor = myIFrame.contentWindow.document;
Editor.designMode = 'On';
Editor.body.co`enter code here`ntentEditable = 'true';
}
else if(browser == "Microsoft Internet Explorer") {
frames['iView'].document.designMode='On';
Editor = frames['iView'].document;
}
you should drop the browser sniffing and do something like
if ( myIFrame && myIFrame.contentWindow ) {
Editor = myIFrame.contentWindow.document;
Editor.designMode = 'On';
}
else if( frames['iView'] ) {
frames['iView'].document.designMode='On';
Editor = frames['iView'].document;
}
Hope that helps :)

before giving an answer I want to be sure about what you are asking. The script is testing Netscape or IE. Opening dragonfly console, you can discover the values of your script. I have the feeling you will have difficulties with Webkit too.
>>> var browser = navigator.appName
undefined
>>> browser
"Opera"

ive noted that the newer versions of opera appear as NETSCAPE when you ask for it with navigator.appName. i remember that was not always so because i also got some older "ifs" around my source where i used the regular name. cant say since when this is, just saying.

Related

XMLHttpRequest status between firefox and chrome

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
createPopup(this);
}
else if (this.status == 404) {
alert("file not found from load");
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
Hi, I am learning about html and css and now javaScript with Dom.
I am trying to parse xml file and know that I have to use XMLHttpRequest to get the data.
To make exception handling such as "there is no file", "xml has fault(wrong xml)", I am trying to use the XMLHttpRequest's member variables "readyStatus", "status" to figure out what status of the result.
If there is another way to deal with this problem, let me know..
First, the chrome doesn't give the "status" value whereas the firefox give with same code. but it is limited to give status == 200 when the file exist regardless of file's status(wrong or not), do you know why?
Second, How can I see "status == 404" using status, could you tell me when it occur?
"Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/" ... This appear in the alert of the Chrome Console...I have the same problem...

How to clear state when statesave page no longer exists in datatables?

My datatables is declared in this way:
"processing": true
,"serverSide": true
,"stateSave":true
,"stateDuration": 60*60*24
,"ajax":{
"url":"/approval/search.json"
,"type":"post"
}
My code to clear state:
if(page doesn't exist){
table.state.clear();
table.draw()
}
I don't know how to tell if page doesn't exist. I've tried table.page.info() but the data it shows is the same whether the page has info or not.
After SO MUCH trial and error I finally succeeded with this:
,"fnDrawCallback":function(){
if(table.row().data()===undefined){
table.state.clear();
location.href=url
}
}
Updated answer for newer versions of datatables:
drawCallback: function (settings) {
let api = this.api();
// fix pagination if saved page is empty
if (api.page() > 0 && api.rows({page: 'current'}).count() === 0) {
api.page('previous').state.save();
location.reload();
}
}
This will set the state to the previous page if the current does not exist, and reload the page.
I may have used the previous answer and alter it. It work perfectly for me :
drawCallback: function (settings) {
let api = this.api();
var info = api.page.info();
if (info.pages!=0 && (api.page() > 0 && api.rows({page: 'current'}).count() === 0)) {
api.page('first').state.save();
window.location.reload();
}
}

JS not loading php into div, website specific behaviour

I have two joomla applications se up with exactly the same versions, the same global configuration settings, and I set up a test application with the following function to load a php into a div on a specific page..
function getDiv(str) {
var id = document.getElementById("appselector").value;
if (id == "") {
document.getElementById("scoffitcategoryedit").innerHTML = "";
return;
}
else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp3 = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp3 = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp3.onreadystatechange = function() {
if (xmlhttp3.readyState == 4 && xmlhttp3.status == 200) {
document.getElementById("scoffitcategoryedit").innerHTML = xmlhttp3.responseText;
alert("WTF");
}
};
xmlhttp3.open("GET","index.php?option=com_jumi&fileid=23& format=raw&" + str + id, true);
xmlhttp3.send();
}
}
I know its a little long winded, but the problem is that the script works fine in one website, but not in the other. Both of them are loading the same versions of jquery (which as you can see I don't like using), and both have the same templates. The only clue I seem to have found is that when I have text links elsewhere on the site attached with query statements pointing to self (index.php?blastr=bla&drivelstr=drivel), the browser shows index.php/ prepended to the link mentioned above.
I debugged the script and in the non functioning website it hangs on the xmlhttp3.send line.
I know people espouse the beauty of jquery's load() function as a replacement for this, but I cant get it to work (probably because the templates are using jquery versions higher than 1.8.1 when it was deprecated). So i'd rather stick with base js.
Any ideas about this inconsistent behaviour ?
This should be a comment but can't yet :(
I would check your configuration file for this line:
public $live_site = '';
and see if there is something in the failing site besides ''.
I found out what it was, I must have actually installed two different versions of the Jumi application in my websites. one website had a file under components/com_jumi/views/application named view.raw.php while the other did not. This meant that the format=raw in the index.php string could not be interpreted and caused a null response.
I simply copied the file into the other website where it was missing and everything then worked fine. But thanks for the response.

Safari analog of chrome.tabs.update or loadURI

I need analog of Chrome chrome.tabs.update or Firefox loadURI in Safari.
I've tried safari.application.activeBrowserWindow.activeTab.url = newURL but it doesn't work with bookmarklets urls like javascript:...
That's right, it doesn't work. You're going to have to pass a message to an injected script in a web page to open the bookmarklet. For example:
in the global page's script:
var activeTab = safari.application.activeBrowerWindow.activeTab;
var url = "javascript:alert('hello sucker')";
activeTab.page.dispatchMessage("pleaseLoadThisUrl", url);
in the injected script:
safari.self.addEventListener("message", handleMessage, false);
function handleMessage(e) {
if (e.name == "pleaseLoadThisUrl") {
window.location = e.message;
}
}
That will work.

Detecting browser print event

Is it possible to detect when a user is printing something from their browser?
To complicate matters, if we are presenting a user with a PDF document in a new window is it possible to detect the printing of that document ( assuming the user prints it from the browser window)?
The closest I've been able to find is if we implement custom print functionality (something like this) and track when that is invoked
I'm primarily interested in a solution that works for internet explorer (6 or later)
You can now detect a print request in IE 5+, Firefox 6+, Chrome 9+, and Safari 5+ using the following technique:
(function() {
var beforePrint = function() {
console.log('Functionality to run before printing.');
};
var afterPrint = function() {
console.log('Functionality to run after printing');
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
}());
I go into more detail into what this is doing and what it can be used for at http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/.
For Internet Exploder, there are the events window.onbeforeprint and window.onafterprint but they don't work with any other browser and as a result they are usually useless.
They seem to work exactly the same for some reason, both executing their event handlers before the printing window opens.
But in case you want it anyway despite these caveats, here's an example:
window.onbeforeprint = function() {
alert("Printing shall commence!");
}
For anyone reading this on 2020.
The addListener function is mostly deprecated in favor of addEventListener except for Safari:
if (window.matchMedia) {
const media = window.matchMedia("print");
const myFunc = mediaQueryList => {
if (mediaQueryList.matches) {
doStuff();
}
};
try {
media.addEventListener("change", myFunc);
} catch (error) {
try {
media.addListener(myFunc);
} catch (error) {
console.debug('Error', error)
}
}
}
Reference: This other S.O question
If it's only for tracking purposes, perhaps you could set a background url in CSS print media to a server page (.aspx, .php, etc) and then do something on the server?
This guy claims it works.
This is not as versitile as TJ's solution, but it may be less buggy (see TJs blog post for issues he found) when only tracking is needed.