D
D
dnv7772012-02-02 12:05:40
JavaScript
dnv777, 2012-02-02 12:05:40

Firefox. HTML5. JS element access by ID

There is a code

<!DOCTYPE html >
<html><body>
   <div id='dv'></div>
</body></html>
<script>
  dv.innerHTML='hello';
</script>


all browsers except FF work correctly. in FF the only way is
document.getElementID("dv").innerHTML='hello';
although if I disable Doctype, then everything works.

I wanted to translate the site to HTML5, and then I met such a surprise from FF. it is not realistic to remake all site scripts for this joke.

what is the problem?
FF stands last


question removed. wrapping everything in jQuery and document.getElementID

Answer the question

In order to leave comments, you need to log in

10 answer(s)
D
dnv777, 2012-02-03
@dnv777

I wrote a macro for VS.NET here to wrap elements in jQuery, bringing them into a normal "hello 90s" look. maybe it will be useful for someone too.
Private Sub Wrap(ByVal prefix, ByVal sufix)
Dim t As EnvDTE.TextSelection = DTE.ActiveDocument.Selection
If t.Text.Length = 0 Then
t.WordLeft()
t.WordRight(True)
End If
t.Text = prefix & t.Text.Trim & sufix
End Sub
Public Sub Wrap_jQuery() ' hotkey Alt+J
'оборачиваем obj в $('#obj')
Wrap("$('#", "')")
End Sub
Public Sub Wrap_e() ' hotkey Alt+E
'в js скрипт: function $e(name) { return typeof (name) === 'string' ? document.getElementById(name) : name; }
Wrap("$e('", "')")
End Sub

D
dnv777, 2012-02-02
@dnv777

can someone answer me why an element in FF cannot be accessed directly?
without comments about the validity of html5, etc.?

V
venn, 2012-02-02
@venn

Here you can read about your problem.

E
egorinsk, 2012-02-03
@egorinsk

> I wanted to convert the site to HTML5
> accessing the DOM element through the window property
Instead of the “html” tag, write “html5”, then it will definitely work.

K
Konstantin Kitmanov, 2012-02-02
@k12th

The script after /html is not valid. Therefore, it does not work in html5, only in quirksmode.
dv.innerHTML='hello';The 90s are long gone.
If you have everything in this form, then yes, it will be difficult to switch to something modern (

K
Kirill Firsov, 2012-02-02
@Isis

Insert the script in the head tag or before closing the body.
getelementbyid.ru/

D
dnv777, 2012-02-02
@dnv777

understandably. but not convenient... all browsers understand and FF is not
in VisualStudio through intellisense, you can quickly type object names, and now you have to wrap each element in a custom $e() or in jquery

E
Evengard, 2012-02-02
@Evengard

Wow. To be honest, I thought so for a long time in all browsers ... No, this antiquity is still preserved. Wow.
Use
var dv = document.getElementById('dv');

D
dnv777, 2012-02-02
@dnv777

yeah, stop downvoting me.
I realized my problem and closed the topic ...

P
Pavlo Ponomarenko, 2012-02-03
@TheShock

in general, I completely agree with all the commentators who spoke above.
If you switch to modern technologies, then switch completely. Don't move - don't move.
If for some reason this is unrealistically important, then you can (although I highly recommend not) do this ugly hack:

var all = document.getElementsByTagName('*');
for (var i = all.length; i--;) if (all[i].id) {
  document[all[i].id] = all[i];
}

True, it is worth considering that elements that are added dynamically (after starting the script) will not be received this way.
But you must understand that you are using such an outdated standard that very soon your site will stop working in a huge number of browsers. Better switch to modern technology now, before it's too late.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question