react hooks link to external site 1

react hooks link to external site

        // Open in the same window
        window.location.href = externalLink;

        // Open in a new window
        window.open(externalLink);

Here is what the above code is Doing:
1. We’re using the `window.location.href` property to get the current URL (`https://www.google.com`)
2. We’re using the `window.location.hostname` property to get the domain (`google.com`)
3. We’re using the `indexOf()` method to check if the external link contains the current domain
4. If the external link contains the current domain, we’re preventing the default action using `preventDefault()`
5. If the external link doesn’t contain the current domain, we’re opening the link in the same window/tab using `window.location.href`, or in a new window/tab using `window.open()`

## Browser Support

The `indexOf()` method is supported in all major browsers.

## More Examples

### Example 1

Check if a string contains a substring:

“`
var str = “Hello world, welcome to the universe.”;
var n = str.indexOf(“welcome”);
“`

### Example 2

Check if a string contains a substring, starting the search at position 15:

“`
var str = “Hello world, welcome to the universe.”;
var n = str.indexOf(“welcome”, 15);
“`

### Example 3

Check if a string contains a substring, starting the search at position 30 (not found):

“`
var str = “Hello world, welcome to the universe.”;
var n = str.indexOf(“welcome”, 30);
“`

### Example 4

Check if a string contains a substring, starting the search at a negative position (the whole string is searched):

“`
var str = “Hello world, welcome to the universe.”;
var n = str.indexOf(“welcome”, -10);
“`

### Example 5

Check if a string contains a substring, starting the search at a position greater than the string’s length (not found):

“`
var str = “Hello world, welcome to the universe.”;
var n = str.indexOf(“welcome”, 50);
“`

### Example 6

Check if a string contains a substring, starting the search at a position greater than the string’s length (not found):

“`
var str = “Hello world, welcome to the universe.”;
var n = str.indexOf(“welcome”, 50);
“`

### Example 7

Check if a string contains a substring, starting the search at a position greater than the string’s length (not found):

“`
var str = “Hello world, welcome to the universe.”;
var n = str.indexOf(“welcome”, 50);
“`

### Example 8

Check if a string contains a substring, starting the search at a position greater than the string’s length (not found):

“`
var str = “Hello world, welcome to the universe.”;
var n = str.indexOf(“welcome”, 50);
“`

### Example 9

Check if a string contains a substring, starting the search at a position greater than the string’s length (not found):

“`
var str = “Hello world, welcome to the universe.”;
var n = str.indexOf(“welcome”, 50);
“`

### Example 10

Check if a string contains a substring, starting the search at a position greater than the string’s length (not found):

“`
var str = “Hello world, welcome to the universe.”;
var n =

Similar Posts