snipt

Ctrl+h for KB shortcuts

JavaScript

Detect Elements Wider than the Screen

1
2
3
4
5
6
7
8
var docWidth = document.documentElement.offsetWidth,
  elements = document.querySelectorAll('*');

[].forEach.call( elements, function(el) {
    if (el.offsetWidth > docWidth) {
        console.log(el);
    }
});
https://snipt.net/embed/e01228604c6f70a45fbd26316678af86/
https://snipt.net/raw/e01228604c6f70a45fbd26316678af86/
e01228604c6f70a45fbd26316678af86
js
JavaScript
8
2016-09-22T16:41:12
True
False
False
Feb 16, 2016 at 11:14 AM
/api/public/snipt/144209/
detect-elements-wider-than-the-screen
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="kd">var</span> <span class="nx">docWidth</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">documentElement</span><span class="p">.</span><span class="nx">offsetWidth</span><span class="p">,</span> </span><span id="L-2"><a name="L-2"></a> <span class="nx">elements</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">querySelectorAll</span><span class="p">(</span><span class="s1">&#39;*&#39;</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="p">[].</span><span class="nx">forEach</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span> <span class="nx">elements</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">el</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">if</span> <span class="p">(</span><span class="nx">el</span><span class="p">.</span><span class="nx">offsetWidth</span> <span class="o">&gt;</span> <span class="nx">docWidth</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-6"><a name="L-6"></a> <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">el</span><span class="p">);</span> </span><span id="L-7"><a name="L-7"></a> <span class="p">}</span> </span><span id="L-8"><a name="L-8"></a><span class="p">});</span> </span></pre></div> </td></tr></table>
detect

PHP

auto detect site_url

1
2
3
4
5
6
<?php

// auto detect base url
$site_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
$site_url .= '://'. $_SERVER['HTTP_HOST'];
$site_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
https://snipt.net/embed/3d238f43ffa8a9abb02d6b1598a23ed8/
https://snipt.net/raw/3d238f43ffa8a9abb02d6b1598a23ed8/
3d238f43ffa8a9abb02d6b1598a23ed8
php
PHP
6
2016-09-24T16:22:01
True
False
False
/api/public/snipt/57830/
auto-detect-site_url
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="c1">// auto detect base url</span> </span><span id="L-4"><a name="L-4"></a><span class="nv">$site_url</span> <span class="o">=</span> <span class="nb">isset</span><span class="p">(</span><span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">&#39;HTTPS&#39;</span><span class="p">])</span> <span class="o">&amp;&amp;</span> <span class="nb">strtolower</span><span class="p">(</span><span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">&#39;HTTPS&#39;</span><span class="p">])</span> <span class="o">!==</span> <span class="s1">&#39;off&#39;</span> <span class="o">?</span> <span class="s1">&#39;https&#39;</span> <span class="o">:</span> <span class="s1">&#39;http&#39;</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a><span class="nv">$site_url</span> <span class="o">.=</span> <span class="s1">&#39;://&#39;</span><span class="o">.</span> <span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">&#39;HTTP_HOST&#39;</span><span class="p">];</span> </span><span id="L-6"><a name="L-6"></a><span class="nv">$site_url</span> <span class="o">.=</span> <span class="nb">str_replace</span><span class="p">(</span><span class="nb">basename</span><span class="p">(</span><span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">&#39;SCRIPT_NAME&#39;</span><span class="p">]),</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">&#39;SCRIPT_NAME&#39;</span><span class="p">]);</span> </span></pre></div> </td></tr></table>
auto, base_url, detect, site_url

PHP

detect iphone browser

1
2
3
4
< ?php
$browser = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
if ($browser == true)  { echo 'Code You Want To Execute'; }
?>
https://snipt.net/embed/94e9ef8d3e0da48dad0b4df25019e76b/
https://snipt.net/raw/94e9ef8d3e0da48dad0b4df25019e76b/
94e9ef8d3e0da48dad0b4df25019e76b
php
PHP
4
2016-09-29T05:43:56
True
False
False
/api/public/snipt/25064/
detect-iphone-browser-1
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="x">&lt; ?php</span> </span><span id="L-2"><a name="L-2"></a><span class="x">$browser = strpos($_SERVER[&#39;HTTP_USER_AGENT&#39;],&quot;iPhone&quot;);</span> </span><span id="L-3"><a name="L-3"></a><span class="x">if ($browser == true) { echo &#39;Code You Want To Execute&#39;; }</span> </span><span id="L-4"><a name="L-4"></a><span class="x">?&gt;</span> </span></pre></div> </td></tr></table>
browser, detect, iphone, php
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.